Back/Forward Cache (bfcache)
bfcache freezes a page's entire state in RAM (DOM, V8 heap, scroll position) when the user navigates away. Pressing Back/Forward restores the page in milliseconds without network requests or re-running JS.
Page Lifecycle in bfcache
Observe JS heap freezing and lifecycle events emitted during Back navigation.
User clicks an external link. The engine checks for blockers (unload, Cache-Control: no-store).
window.addEventListener('pagehide', (e) => console.log(e.persisted));- eligible
- true
- event
- pagehide (persisted: true)
- jsTimers
- Freezing
Under the Hood
Unlike the HTTP disk cache (which only saves raw response bytes and forces a full rebuild), bfcache acts like a laptop's hibernate-to-RAM: the entire DOM, Browser Rendering Pipeline state, and V8 heap are snapshotted intact and thawed on return.
- On outbound navigation, the engine checks for blockers (
unloadlisteners, open WebSockets/IndexedDB transactions,Cache-Control: no-store). - If eligible, the engine fires
pagehidewithevent.persisted === trueand freezes all Event Loop task queues and timers. - On Back/Forward navigation, the memory snapshot is thawed and
pageshow(event.persisted === true) fires without re-firingDOMContentLoadedorload.
window.addEventListener('pageshow', (event) => {
if (event.persisted) {
// Page was thawed from bfcache: refresh volatile auth or cart state
refreshSessionBanner();
}
});Problem Genealogy
- 2005 (HTTP Cache only): Back navigation re-parsed HTML and re-executed JS from disk cache.
- 2019 (Multi-process bfcache): Re-architected in Chrome/Firefox/Safari to isolate and freeze each origin in memory.
- 2024+ (
unloadDeprecation): Mandatory migration fromunloadtopagehide/visibilitychange, because nothing says "legacy enterprise telemetry" quite like a 3-lineunloadscript nuking instant back-navigation site-wide.
Production Pitfalls
unloadlistener in a third-party script: A singlewindow.addEventListener('unload', ...)disables bfcache across the entire site.- Stale state on return: A shopping cart or CSRF token restored from bfcache remains frozen; listen to
pageshow(if (e.persisted)) to refresh volatile data.
- When not to use bfcache: On banking or authenticated views displaying sensitive data after logout, enforce
Cache-Control: no-storeor clear sensitive DOM onpagehide.
Connections & Logical Next Steps
Using visibilitychange and pagehide instead of unload keeps pages eligible for bfcache.
RUM scripts must flush on visibilitychange/pagehide instead of unload to preserve bfcache.
While cached in bfcache, JS tasks and timers are frozen and resumed upon restoration.
Freezes the full DOM, layout tree, and compositor state in RAM to restore frames without re-parsing.
Same-document pushState/replaceState updates mutate session history entries without triggering full document unloads.
Disk cache stores raw HTTP bytes; no-store disables both disk cache and in-memory bfcache.
bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.
Seen in Architecture Autopsies
Mechanical Comparisons
Disk cache stores raw HTTP bytes; no-store disables both disk cache and in-memory bfcache.
bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.