Created
May 14, 2020 05:35
-
-
Save claus/992a5596d6532ac91b24abe24e10ae81 to your computer and use it in GitHub Desktop.
Restore scroll position after navigating via browser back/forward buttons in Next.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import useScrollRestoration from "utils/hooks/useScrollRestoration"; | |
const App = ({ Component, pageProps, router }) => { | |
useScrollRestoration(router); | |
return <Component {...pageProps} />; | |
}; | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react'; | |
import Router from 'next/router'; | |
function saveScrollPos(url) { | |
const scrollPos = { x: window.scrollX, y: window.scrollY }; | |
sessionStorage.setItem(url, JSON.stringify(scrollPos)); | |
} | |
function restoreScrollPos(url) { | |
const scrollPos = JSON.parse(sessionStorage.getItem(url)); | |
if (scrollPos) { | |
window.scrollTo(scrollPos.x, scrollPos.y); | |
} | |
} | |
export default function useScrollRestoration(router) { | |
useEffect(() => { | |
if ('scrollRestoration' in window.history) { | |
let shouldScrollRestore = false; | |
window.history.scrollRestoration = 'manual'; | |
restoreScrollPos(router.asPath); | |
const onBeforeUnload = event => { | |
saveScrollPos(router.asPath); | |
delete event['returnValue']; | |
}; | |
const onRouteChangeStart = () => { | |
saveScrollPos(router.asPath); | |
}; | |
const onRouteChangeComplete = url => { | |
if (shouldScrollRestore) { | |
shouldScrollRestore = false; | |
restoreScrollPos(url); | |
} | |
}; | |
window.addEventListener('beforeunload', onBeforeUnload); | |
Router.events.on('routeChangeStart', onRouteChangeStart); | |
Router.events.on('routeChangeComplete', onRouteChangeComplete); | |
Router.beforePopState(() => { | |
shouldScrollRestore = true; | |
return true; | |
}); | |
return () => { | |
window.removeEventListener('beforeunload', onBeforeUnload); | |
Router.events.off('routeChangeStart', onRouteChangeStart); | |
Router.events.off('routeChangeComplete', onRouteChangeComplete); | |
Router.beforePopState(() => true); | |
}; | |
} | |
}, [router]); | |
} |
The hook version from @MichalSzorad seems to work fairly nicely.
I'm not sure if there's a possible solution, that would be worth the complexity, but one scenario I don't see addressed is as follows:
- Scroll down page A click on link to page B
- On page B click on a link that goes back to page A
- We land and stay at the top of page A
- Click back button twice, scroll is restored to the scrolled down positions, as it was on first view of page A
- Click forward button twice, scroll is again restored to the scrolled down position, even though we had not originally scrolled this second instance of Page A.
How crazy/possible is it to somehow record the scroll position for each pop state version of a url? If we don't, is it better to reset a saved scroll position after arriving to the page via a regular link again?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works fine for me
Thank you for this resource