Skip to content
Guide

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 unload and beforeunload listeners; they disable bfcache in most browsers.
  • Prefer pagehide/pageshow for cleanup and restore logic.
  • When you need to refresh data on return, check event.persisted on pageshow.
  • Keep timers and subscriptions idempotent so they can be paused and rehydrated.
  • Do not rely on visibilitychange alone; 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 beforeunload or unload listeners on the page.
  • pageshow handler checks event.persisted before refetching.
  • Subscriptions and intervals can safely resume after a restore.
Built as a personal SvelteKit 5 lab with Supabase auth. Guides, patterns, and a playground you can actually ship.
Command Palette
Search for a command to run