Skip to content

Commit

Permalink
Make it an error to use <a> tags without an exemption.
Browse files Browse the repository at this point in the history
Fixes #3911
  • Loading branch information
Zemnmez committed Nov 17, 2023
1 parent 6d3a181 commit db86e25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ declare module '*.jpg' {
export const blurWidth: number;
export const blurHeight: number;
}

type LinkBrand = 'hey. please use project/zemn.me/next/components/Link instead';

declare module 'global' {
global {
namespace JSX {
interface IntrinsicElements {
// Override the `<a>` element
a: { 'data-brand': LinkBrand };
}
}
}
}
12 changes: 11 additions & 1 deletion project/zemn.me/next/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function isFirstPartyURL(u: string | UrlObject | URL): boolean {
);
}

type Exempt<T> = T & { 'data-brand': LinkBrand };

export function Link({ href, rel, target, ...props }: LinkProps) {
if (href !== undefined && !isFirstPartyURL(href)) {
rel = `${rel ?? ''} external`.trim();
Expand All @@ -62,7 +64,15 @@ export function Link({ href, rel, target, ...props }: LinkProps) {
// next Link has a strangely strident stance on providing the href parameter
// which we do not. if there is no href we just fall back to using an <a> tag.
// https://github.com/i18next/next-i18next/issues/599
if (href === undefined) return <a {...{ href, rel, target, ...props }} />;
if (href === undefined) {
const p = {
href,
rel,
target,
...props,
};
return <a {...(props as Exempt<typeof p>)} />;
}
return <NextLink {...{ href, rel, target, ...props }} />;
}

Expand Down

0 comments on commit db86e25

Please sign in to comment.