The Back Button Reloads: Five Root Causes in BFCache Testing
Archi3 min read
Contents
Test Setup: One Page, Three Browsers, A Stopwatch#
I built a page that is deliberately ordinary: 240 KB of HTML, 38 images, two web fonts, a 180 KB script bundle. It was served from a local nginx where I could swap headers between runs. Same laptop, clean profiles in Chromium, Firefox and Safari, and a navigation loop of A to B to back.
The instrumentation was three lines: log pagehide and pageshow with event.persisted, and read performance.getEntriesByType('navigation')[0].type. A restored page reports back_forward with persisted: true. A reload reports back_forward with persisted: false.
When the cache worked, back took 12 to 20 ms and the scroll position survived. When it failed, I got a full reload: 540 ms warm, 1.1 s cold. The cause was rarely the network. It was one of five things below.
1. An unload listener is a quiet opt-out#
The unload event fires when a document is being destroyed. A page held in the back/forward cache is not being destroyed, so the browser cannot honor that contract. The cheapest fix is to skip the page entirely.
In my runs, one window.addEventListener('unload', ...) was enough to lose the cache in all three browsers. The listener was usually not mine. An analytics snippet, a tag manager container, or a vendor script bundled two years ago can register it, and you will never see it in your own source. Search the built bundle, not the repository. Replace it with pagehide or visibilitychange, which fire meaningfully in both directions.
2. no-store on the document, not the subresources#
This is the misdiagnosis I see most. Cache-Control: no-store on a JSON API response or an image does not disqualify the page. The header that matters is the one on the HTML document response, because that is the response the browser would have to keep alive.
When I set no-store on the document, Chromium and Firefox both skipped the cache. Safari did the same in my runs. When I set it on the XHR and left the document at private, max-age=0, must-revalidate, the page stayed eligible.
So the rule is narrower than teams assume. Keep no-store for pages where a restored view would leak something sensitive, such as a statement or a session-bound receipt. For an ordinary logged-in page, it is often doing nothing except forcing a refetch.
3. Open sockets, held transactions, taken locks#
A page that holds state the browser cannot reason about does not qualify. In my tests, an open IndexedDB transaction blocked the cache in Chromium and Firefox. A live WebSocket blocked it in Chromium, while Firefox was more forgiving on the socket and less forgiving on the transaction. A held Web Lock blocked all three.
This matters for dashboards and chat UIs, which keep connections open by design. The pattern that worked was to close on pagehide and reopen on pageshow when persisted is true. That is more code, but it is the contract the cache expects.
4. Cross-origin frames can drag the parent down#
One test page embedded a third-party widget. The parent was clean, with no unload handler and no no-store. It still failed. The iframe had registered its own unload listener, and in Chromium that was enough to disqualify the whole tree.
You cannot fix a vendor iframe from your side. You can lazy-load it, put it behind a click, or replace it. On the test page, deferring the embed until after first interaction restored the parent to the cache in every run.
5. Eviction, which looks like a bug#
An eligible page does not stay eligible forever. Browsers evict entries under memory pressure, after time, and when the cache reaches its size limit. In my loop, a page left for several minutes restored fine; one left much longer came back as a reload even though the code had not changed.
That is why a report of "it works on my machine" is often correct and still useless. Test with a stopwatch, not a single click.
What Changed After The Fixes#
I removed two unload listeners, one mine and one from a bundled vendor file. I changed the document header from no-store to private, max-age=0, must-revalidate. I added a pagehide handler that closes the socket, and a pageshow handler that reopens it only when persisted is true. I deferred the iframe.
Back navigation on that page went from 540 ms to 14 ms. The number is less interesting than the location of the fix: none of it was in the network layer, and all of it was in the page lifecycle code.
More articles