Make the Back Button Fast: A Practical Guide to bfcache
- Get link
- X
- Other Apps
Make the Back Button Fast: A Practical Guide to bfcache
When a visitor presses Back or Forward, the browser has two common choices: rebuild the page from the network and JavaScript, or restore it almost instantly from memory. The second choice is the browser's back/forward cache, usually abbreviated as bfcache. It preserves the live page, pauses JavaScript execution, and resumes the page on return.
That sounds simple, but bfcache changes assumptions that many applications quietly rely on: pending timers can resume, a page may outlive navigation for longer than expected, and third-party embeds can affect whether the whole page qualifies. The goal is not merely to get the cache hit. It is to make restoration predictable.
Start by removing the unreliable unload hooks
Do not use unload. Browser behavior for this event is inconsistent, and Firefox, in particular, can make a page ineligible for bfcache when it finds an unload listener. Chrome and Safari have also deprecated or reduced their reliability around it. Use pagehide for cleanup that must run during navigation, but first classify the navigation:
window.addEventListener("pagehide", (event) => { if (event.persisted) { // The browser intends to keep this document in bfcache. closeNonessentialResources(); } else { // The document is likely to be destroyed. cleanupBeforeUnload(); } }); pagehide.persisted === true is an intention, not a guarantee: another browser rule or in-progress operation may still prevent caching.
beforeunload is different. It does not disqualify a page in the same straightforward way in modern browsers, but it should be used only to warn about unsaved changes. Register it conditionally, remove it as soon as the risk disappears, and remember that it cannot safely be treated as a cleanup hook.
function warnAboutUnsavedChanges(event) { event.preventDefault(); event.returnValue = ""; } window.addEventListener("beforeunload", warnAboutUnsavedChanges); // Remove it after the form is saved: window.removeEventListener("beforeunload", warnAboutUnsavedChanges); Refresh state after restoration
The pageshow event fires for normal loads and bfcache restores. Its event.persisted property distinguishes the two. On a restore, use that moment to recheck server authorization, refresh data with stale-time rules, reconnect services, and resume work that should not remain paused.
window.addEventListener("pageshow", async (event) => { if (!event.persisted) return; await refreshPermissions(); await refreshVisibleData(); }); Do not assume every restored application action should silently rerun. A form may contain unsaved text; a search list may be valid from memory; an order screen may need fresh inventory. The cache restores a snapshot, so state reconciliation is an application decision rather than a generic reload.
Test actual navigations, not just a DevTools hint
- Open the target page in Chrome or another supported browser.
- Navigate to a second page on your site or an external page.
- Press Back and watch the
pageshowevent. - If Chrome's bfcache tooling is available, inspect the Back/Forward Cache section for the exact blocking reason.
- Repeat with a third-party iframe and a completed versus in-progress IndexedDB operation.
Do not use Cache-Control: no-store as a blanket performance optimization. It is appropriate for sensitive pages that should not be cached, but it also reduces bfcache eligibility when browsers refuse to cache the page resource. For ordinary pages, prefer narrower cache controls and ensure dynamic content has an explicit freshness policy.
For ongoing diagnostics, Chromium exposes PerformanceNavigationTiming.notRestoredReasons. It can report reasons at the top level and for same-origin iframe children, but the API is experimental and browser support is not universal. Treat it as supplementary instrumentation, not as proof that a page is eligible everywhere.
const entries = performance.getEntriesByType("navigation"); const reasons = entries[0]?.notRestoredReasons; if (reasons) { console.log(reasons); } A short bfcache checklist
- Remove
unloadlisteners and libraries that add them. - Add conditional
beforeunloadhandling only for real unsaved work. - Use
pagehideandpageshow.persistedfor lifecycle transitions. - Close or reconnect resources where stale state could be harmful.
- Check third-party frames, open database transactions, and privacy headers.
- Verify restores with real Back/Forward navigation and browser-specific diagnostics.
bfcache is easy to describe and surprisingly easy to disrupt. The practical win comes from making navigation lifecycle behavior explicit, testing the restore path, and fixing the specific browser reason rather than treating every missing cache hit as a JavaScript defect.
References
- web.dev, “Back/forward cache” - browser behavior,
pageshow/pagehide, bfcache eligibility, analytics, andno-storeguidance. - MDN, “bfcache” - overview of in-memory page snapshots and the resource and compatibility implications.
- MDN, “PageTransitionEvent” - specification-backed explanation of
persistedforpageshowandpagehide. - MDN, “Monitoring bfcache blocking reasons” -
PerformanceNavigationTiming.notRestoredReasons, limitations, and example monitoring code. - WebKit, “WebKit Page Cache I: The Basics” - an implementation-level explanation of suspending and restoring live page state in the browser engine.
❤️ Support This Blog
If this post helped you, you can support my writing with a small donation. Thank you for reading.
- Get link
- X
- Other Apps

Comments
Post a Comment