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]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm back again. I updated and simplified the above scripts pretty drastically. I'm also using window.history.state.key to track positions, so it supports going to the same page throughout history and always restoring the correct scroll position. This also fixes iOS Snapshots, since I don't set window.scrollRestoration to manual.
I also allow you to set a selector for an element to restore scroll to, for my website I have an "overflow-y-auto" child element that I need scrollRestoration for, so window won't do the trick. Here is my updated, versatile script for 2024 lol.
I also added an option to add a delay if you need to, and I moved router into the options, it will default to useRouter if you don't provide one.
useScrollRestoration.js
Default setup:
useScrollRestoration()
Scroll child element
<main>
with overflow:useScrollRestoration(router, { selector: "main" })
Delay Example:
useScrollRestoration(router, { delay: 100 })