Register the beforeunload listener only while a beforeNavigate callback exists
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
_start_router registers beforeunload unconditionally (packages/kit/src/runtime/client/client.js:3164 at 2eed63867). Firefox excludes any page with a beforeunload listener from the bfcache, so every SvelteKit app loses back/forward cache there even when no beforeNavigate callback exists to run.
The listener exists to fire beforeNavigate with type: 'leave' and to reset history.scrollRestoration; persist_state already also runs on visibilitychange, which MDN recommends for state saving. Add the listener when the first beforeNavigate callback registers, remove it when the last one unregisters, and move the scrollRestoration reset to pagehide.
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#usage_notes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in packages/kit/src/runtime/client/client.js at _start_router and the beforeunload registration around line 3164. Trace where beforeNavigate callbacks register and unregister, then verify the listener is present only while callbacks exist and that scrollRestoration is reset on pagehide. Confirm that persist_state remains covered by visibilitychange and that the no-callback case preserves bfcache eligibility.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, performance, web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100