Skip to content

Commit 0330368

Browse files
committed
Fix fragment link back button issue
- Change fragment links from <a> tags to NextLink components in MDX/Link.tsx - Set history.scrollRestoration = 'auto' for all browsers in _app.tsx - This prevents multiple new pages in backStack and maintains scroll position - Similar to how Wikipedia handles fragment links
1 parent 790625f commit 0330368

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

src/components/MDX/Link.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ function Link({
4444
</ExternalLink>
4545
) : href.startsWith('#') ? (
4646
// eslint-disable-next-line jsx-a11y/anchor-has-content
47-
<a className={cn(classes, className)} href={href} {...props}>
47+
<NextLink className={cn(classes, className)} href={href} {...props}>
4848
{modifiedChildren}
49-
</a>
49+
</NextLink>
5050
) : (
5151
<NextLink href={href} className={cn(classes, className)} {...props}>
5252
{modifiedChildren}

src/pages/_app.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,8 @@ export default function MyApp({Component, pageProps}: AppProps) {
3333
const router = useRouter();
3434

3535
useEffect(() => {
36-
// Taken from StackOverflow. Trying to detect both Safari desktop and mobile.
37-
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
38-
if (isSafari) {
39-
// This is kind of a lie.
40-
// We still rely on the manual Next.js scrollRestoration logic.
41-
// However, we *also* don't want Safari grey screen during the back swipe gesture.
42-
// Seems like it doesn't hurt to enable auto restore *and* Next.js logic at the same time.
43-
history.scrollRestoration = 'auto';
44-
} else {
45-
// For other browsers, let Next.js set scrollRestoration to 'manual'.
46-
// It seems to work better for Chrome and Firefox which don't animate the back swipe.
47-
}
36+
// Set scroll restoration to 'auto' for all browsers to fix fragment link back button issue
37+
history.scrollRestoration = 'auto';
4838
}, []);
4939

5040
useEffect(() => {

0 commit comments

Comments
 (0)