The bfcache keeps a complete in-memory snapshot of a page, including its JS heap and DOM, when the user navigates away. Pressing Back or Forward then restores it instantly, with no network, no parsing and no re-running scripts. Back and forward navigations are a large share of all navigations (Chrome has reported roughly 1 in 10 on desktop and 1 in 5 on mobile), so bfcache eligibility is one of the cheapest big wins for real-user LCP. Pages become ineligible because of things like unload handlers, open connections (WebSocket, WebRTC, in-progress IndexedDB transactions), window.opener references and, historically in Chromium, Cache-Control: no-store.
window.addEventListener('pagehide', (e) => {
// e.persisted === true → page may be going into bfcache
flushAnalytics();
});
window.addEventListener('pageshow', (e) => {
if (e.persisted) {
// Restored from bfcache: JS state is intact, but the world may have changed
refreshStaleData(); // cart count, notifications, auth state
reconnectWebSocket();
reportVisit({ type: 'back_forward_cache' });
}
});
Use pagehide and visibilitychange, never unload, for cleanup and beaconing. Firefox historically disqualified pages with unload listeners from bfcache, and Chromium is phasing unload out entirely. A Permissions-Policy: unload=() header lets you block unload handlers, including ones added by third-party scripts.
| Blocker | Fix |
|---|---|
unload listeners (yours or third-party) | Switch to pagehide; use Permissions-Policy: unload=() |
| Open WebSocket / WebRTC / BroadcastChannel | Close on pagehide, reopen on pageshow |
| Unfinished IndexedDB transactions, held Web Locks | Finish or release before leaving |
window.opener relationships | rel="noopener" (the default for target=_blank now) |
Cache-Control: no-store on the HTML | Chromium has historically blocked these and has been experimenting with relaxing it; avoid no-store unless the page is truly sensitive |
beforeunload registered permanently | Add it only while there are unsaved changes |
Pages opened by or controlling other windows, in-progress fetches with keepalive | Case by case |
notRestoredReasons on the navigation timing entry (Chromium) reports in the field why a back navigation wasn't restored, including reasons caused by iframes.const nav = performance.getEntriesByType('navigation')[0];
if (nav.type === 'back_forward' && nav.notRestoredReasons) {
report(nav.notRestoredReasons);
}
pageshow with persisted divided by all back/forward navigations.pageshow with persisted, or show a light "updated" state.pageshow.no-store) or clear the screen on pagehide.pagehide/pageshow with persisted, and why unload is harmful.notRestoredReasons for field data.Spotted a mistake or a rights concern? Report an issue