Back/Forward Cache (bfcache)
In-memory cache snapshotting the full JS heap and DOM tree on outbound navigation to restore the page instantaneously on Back/Forward.
About this page
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.
Where this connects
Prerequisites
Event Loop, Macrotasks, and Microtask Queue
While cached in bfcache, JS tasks and timers are frozen and resumed upon restoration.
Browser Rendering Pipeline (Critical Rendering Path)
Freezes the full DOM, layout tree, and compositor state in RAM to restore frames without re-parsing.
Next & related
Beacon API and visibilitychange Lifecycle
Using visibilitychange and pagehide instead of unload keeps pages eligible for bfcache.
PerformanceObserver & RUM Telemetry
RUM scripts must flush on visibilitychange/pagehide instead of unload to preserve bfcache.
Seen in Architecture Autopsies
Autopsy #04: The Morning Yesterday's Bundle Corrupts Today's Database
Restoring a page from the back-forward cache resumes an old JS runtime after IndexedDB already upgraded.
Autopsy #14: The Schema Migration That Froze Every Open Tab
Pages holding open IndexedDB connections block bfcache or stall versionchange handshakes unless db.close() runs on pagehide.
Mechanical Comparisons
- Contrasts WithBack/Forward Cache (bfcache) vs HTTP Caching Directives
Disk cache stores raw HTTP bytes; no-store disables both disk cache and in-memory bfcache.
- Contrasts WithSpeculation Rules & Prerendering vs Back/Forward Cache (bfcache)
bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.