Skip to content

Commit 4778d27

Browse files
gaearonclaude
andcommitted
Resolve slash-less content links against the site root
A dozen content links are written like [Setup](learn/setup). The Pages Router's Link resolved those against the origin, so they rendered as /learn/setup; the App Router's Link leaves them relative to the current page (/learn/learn/setup -> 404). Restore the old behavior. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d723be6 commit 4778d27

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

src/components/MDX/Link.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ function MDXLink({
3535
if (!href) {
3636
return <a href={href} className={className} {...props} />;
3737
}
38+
if (href.startsWith('https://')) {
39+
return (
40+
<ExternalLink href={href} className={cn(classes, className)} {...props}>
41+
{modifiedChildren}
42+
</ExternalLink>
43+
);
44+
}
45+
// Some content links to other pages without a leading slash, e.g.
46+
// [Setup](learn/setup). The Pages Router resolved those against the site
47+
// root; the App Router's Link would resolve them against the current page.
48+
const internalHref = /^[a-z]/i.test(href) ? '/' + href : href;
3849
return (
39-
<>
40-
{href.startsWith('https://') ? (
41-
<ExternalLink href={href} className={cn(classes, className)} {...props}>
42-
{modifiedChildren}
43-
</ExternalLink>
44-
) : (
45-
<Link href={href} className={cn(classes, className)} {...props}>
46-
{modifiedChildren}
47-
</Link>
48-
)}
49-
</>
50+
<Link href={internalHref} className={cn(classes, className)} {...props}>
51+
{modifiedChildren}
52+
</Link>
5053
);
5154
}
5255

0 commit comments

Comments
 (0)