bfcache-ready SvelteKit pages
The browser back/forward cache (bfcache) keeps a full page snapshot in memory so back/forward navigation feels instant. Eligibility depends on the page lifecycle, not SvelteKit itself. These steps keep your routes bfcache-friendly.
Best practices
- Avoid
unloadandbeforeunloadlisteners; they disable bfcache in most browsers. - Prefer
pagehide/pageshowfor cleanup and restore logic. - When you need to refresh data on return, check
event.persistedonpageshow. - Keep timers and subscriptions idempotent so they can be paused and rehydrated.
- Do not rely on
visibilitychangealone; it fires on bfcache restores but is not the canonical signal. - Avoid forced full reloads on back/forward navigation unless the data must be real-time.
SvelteKit pattern
<svelte:window
on:pagehide={() => pauseRealtime()}
on:pageshow={(event) => {
if (event.persisted) {
resumeRealtime();
refreshIfStale();
}
}}
/> Checklist
- No
beforeunloadorunloadlisteners on the page. pageshowhandler checksevent.persistedbefore refetching.- Subscriptions and intervals can safely resume after a restore.