Skip to content

Commit c0da5c2

Browse files
committed
fix: correct scroll restoration on page reload (Fixes #1226)
1 parent f44e570 commit c0da5c2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
transpilePackages: ['@primer/react', '@tabnews/ui', '@tabnews/forms'],
33
experimental: {
4-
scrollRestoration: true,
4+
scrollRestoration: false,
55
},
66
// Workaround: https://github.com/vercel/next.js/issues/51478#issuecomment-2095745187
77
pageExtensions: ['public.js', 'workaround.js'],

pages/_app.public.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RevalidateProvider } from 'next-swr';
2+
import { useEffect } from 'react';
23
import { SWRConfig } from 'swr';
34
import '@tabnews/ui/css';
45

@@ -20,6 +21,18 @@ async function SWRFetcher(resource, init) {
2021
const fallbackData = { body: null, headers: {} };
2122

2223
function MyApp({ Component, pageProps }) {
24+
useEffect(() => {
25+
const handleBeforeUnload = () => {
26+
sessionStorage.setItem('scrollPos', window.scrollY);
27+
console.warn('Posição do Scroll SALVA:', window.scrollY);
28+
};
29+
30+
window.addEventListener('beforeunload', handleBeforeUnload);
31+
32+
return () => {
33+
window.removeEventListener('beforeunload', handleBeforeUnload);
34+
};
35+
}, []);
2336
return (
2437
<ThemeProvider>
2538
<Turnstile />

pages/interface/components/Markdown/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { isTrustedDomain } from '@tabnews/helpers';
22
import { MarkdownEditor, MarkdownViewer } from '@tabnews/ui/markdown';
3+
import { useEffect } from 'react';
34

45
const shouldAddNofollow = (url) => !isTrustedDomain(url);
56

67
export default function Viewer(props) {
8+
useEffect(() => {
9+
const savedPos = sessionStorage.getItem('scrollPos');
10+
11+
if (savedPos) {
12+
console.warn('Posição do Scroll RESTAURADA:', savedPos);
13+
14+
window.scrollTo(0, parseInt(savedPos, 10));
15+
16+
sessionStorage.removeItem('scrollPos');
17+
}
18+
}, [props.content]);
719
return MarkdownViewer({
820
shouldAddNofollow,
921
...props,

0 commit comments

Comments
 (0)