Repository navigation
fix: restore scroll position and loaded pages after reload #3741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fallenbagel
wants to merge
1
commit into
develop
Choose a base branch
from
fix/discover-scroll-restore
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| import { useRouter } from 'next/router'; | ||
| import { useEffect, useLayoutEffect, useRef } from 'react'; | ||
|
|
||
| const ENTRIES_KEY = 'scroll-restoration'; | ||
| const HIDDEN_ENTRY_KEY = 'scroll-restoration-hidden-entry'; | ||
| const CHECK_INTERVAL_MS = 100; | ||
| const SETTLE_MS = 1000; | ||
| const MAX_WAIT_MS = 10000; | ||
| const INPUT_EVENTS = ['keydown', 'pointerdown', 'wheel']; | ||
| const KEY_REFRESH_EVENTS = ['keydown', 'pointerdown', 'scroll']; | ||
|
|
||
| interface ScrollEntry { | ||
| url: string; | ||
| y: number; | ||
| pages?: number; | ||
| } | ||
|
|
||
| let listPages: number | undefined; | ||
| let pageRestore: { url: string; pages: number } | undefined; | ||
|
|
||
| // sessionStorage throws when site data is blocked or the quota is full | ||
| const tryStorage = <T>(access: () => T): T | undefined => { | ||
| try { | ||
| return access(); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| const readEntries = (): Record<string, ScrollEntry> => | ||
| tryStorage(() => JSON.parse(sessionStorage.getItem(ENTRIES_KEY) ?? '{}')) ?? | ||
| {}; | ||
|
|
||
| export const usePageCountRestoration = ( | ||
| size: number, | ||
| setSize: (size: number) => void, | ||
| isLoading: boolean | ||
| ) => { | ||
| const router = useRouter(); | ||
|
|
||
| useEffect(() => { | ||
| listPages = size; | ||
| return () => { | ||
| listPages = undefined; | ||
| }; | ||
| }, [size]); | ||
|
|
||
| useEffect(() => { | ||
| if (pageRestore?.url !== router.asPath || isLoading) { | ||
| return; | ||
| } | ||
|
|
||
| if (size < pageRestore.pages) { | ||
| setSize(pageRestore.pages); | ||
| } else { | ||
| pageRestore = undefined; | ||
| } | ||
| }, [isLoading, router.asPath, setSize, size]); | ||
| }; | ||
|
|
||
| const useScrollRestoration = () => { | ||
| const router = useRouter(); | ||
| const isLandingRef = useRef(true); | ||
| const poppedRef = useRef(false); | ||
|
|
||
| // a layout effect so pageRestore is set before the page's own effects read it | ||
| useLayoutEffect(() => { | ||
| const url = router.asPath; | ||
| const isLanding = isLandingRef.current; | ||
| const popped = poppedRef.current; | ||
| isLandingRef.current = false; | ||
| poppedRef.current = false; | ||
| pageRestore = undefined; | ||
|
|
||
| // routeChangeStart can fire after a back/forward has already moved history.state | ||
| let entryKey: string | undefined = window.history.state?.key; | ||
| let pendingY: number | undefined; | ||
| let timer: ReturnType<typeof setInterval> | undefined; | ||
|
|
||
| const refreshKey = () => { | ||
| if (!poppedRef.current) { | ||
| entryKey = window.history.state?.key; | ||
| } | ||
| }; | ||
| const save = () => { | ||
| if (!entryKey) { | ||
| return; | ||
| } | ||
|
|
||
| const entries = readEntries(); | ||
| entries[entryKey] = { | ||
| url, | ||
| y: pendingY ?? window.scrollY, | ||
| pages: pageRestore?.pages ?? listPages, | ||
| }; | ||
| tryStorage(() => | ||
| sessionStorage.setItem(ENTRIES_KEY, JSON.stringify(entries)) | ||
| ); | ||
| }; | ||
| const saveIfHidden = () => { | ||
| if (document.visibilityState === 'hidden') { | ||
| refreshKey(); | ||
| save(); | ||
| const hiddenKey = entryKey; | ||
| if (hiddenKey) { | ||
| tryStorage(() => sessionStorage.setItem(HIDDEN_ENTRY_KEY, hiddenKey)); | ||
| } | ||
| } | ||
| }; | ||
| const onPopState = () => { | ||
| poppedRef.current = true; | ||
| stop(); | ||
| }; | ||
| const stop = () => { | ||
| clearInterval(timer); | ||
| INPUT_EVENTS.forEach((event) => | ||
| window.removeEventListener(event, cancel) | ||
| ); | ||
| }; | ||
| const done = () => { | ||
| pendingY = undefined; | ||
| stop(); | ||
| }; | ||
| const cancel = () => { | ||
| pageRestore = undefined; | ||
| done(); | ||
| }; | ||
|
|
||
| let entry: ScrollEntry | undefined; | ||
| if (isLanding) { | ||
| // a fresh load opened from another page won't match the hidden URL | ||
| const hiddenKey = tryStorage(() => | ||
| sessionStorage.getItem(HIDDEN_ENTRY_KEY) | ||
| ); | ||
| tryStorage(() => sessionStorage.removeItem(HIDDEN_ENTRY_KEY)); | ||
| entry = hiddenKey ? readEntries()[hiddenKey] : undefined; | ||
| } else if (popped && entryKey) { | ||
| entry = readEntries()[entryKey]; | ||
| } | ||
|
|
||
| if (entry?.url === url && entry.y) { | ||
| const { y } = entry; | ||
| pendingY = y; | ||
| if (entry.pages) { | ||
| pageRestore = { url, pages: entry.pages }; | ||
| } | ||
|
|
||
| const startedAt = Date.now(); | ||
| let height: number | undefined; | ||
| let changedAt: number | undefined; | ||
| const check = () => { | ||
| const now = Date.now(); | ||
| const maxY = document.documentElement.scrollHeight - window.innerHeight; | ||
| // a spinner holds the height still, so settling counts from the first change | ||
| if (height !== undefined && maxY !== height) { | ||
| changedAt = now; | ||
| } | ||
| height = maxY; | ||
|
|
||
| // a list still loading its saved pages has not settled | ||
| const isSettled = | ||
| !pageRestore && | ||
| changedAt !== undefined && | ||
| now - changedAt >= SETTLE_MS; | ||
| if (maxY >= y || isSettled) { | ||
| window.scrollTo(0, y); | ||
| done(); | ||
| } else if ( | ||
| !pageRestore && | ||
| changedAt === undefined && | ||
| now - startedAt >= MAX_WAIT_MS | ||
| ) { | ||
| done(); | ||
| } | ||
| }; | ||
|
|
||
| INPUT_EVENTS.forEach((event) => | ||
| window.addEventListener(event, cancel, { passive: true }) | ||
| ); | ||
| timer = setInterval(check, CHECK_INTERVAL_MS); | ||
| check(); | ||
| } | ||
|
|
||
| router.events.on('routeChangeStart', save); | ||
| window.addEventListener('popstate', onPopState); | ||
| KEY_REFRESH_EVENTS.forEach((event) => | ||
| window.addEventListener(event, refreshKey, { passive: true }) | ||
| ); | ||
| document.addEventListener('visibilitychange', saveIfHidden); | ||
|
|
||
| return () => { | ||
| stop(); | ||
| router.events.off('routeChangeStart', save); | ||
| window.removeEventListener('popstate', onPopState); | ||
| KEY_REFRESH_EVENTS.forEach((event) => | ||
| window.removeEventListener(event, refreshKey) | ||
| ); | ||
| document.removeEventListener('visibilitychange', saveIfHidden); | ||
| }; | ||
| }, [router.asPath, router.events]); | ||
| }; | ||
|
|
||
| export default useScrollRestoration; | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.