Our public pages come back from the Back button in 22ms, and not one page behind our login does
The back/forward cache is the largest free performance win on the web and the easiest one to lose. When a browser can keep a whole document alive in memory while you visit the next page, pressing Back is a resume rather
The back/forward cache is the largest free performance win on the web and the easiest one to lose. When a browser can keep a whole document alive in memory while you visit the next page, pressing Back is a resume rather than a load: no request, no parse, no hydration, scroll position and form values already where you left them.
You cannot find out whether you have it by reading your code. I measured it on Nakodo, which is a Next.js app with 76 public pages in front of a login and about twenty screens behind it, and the answer split exactly along that line.
The test
A bfcache restore keeps the same JavaScript realm alive, so the simplest probe is a marker that can only survive one:
// On the page you are about to leave
window.__stamp = "s7fq2k";
// After going back
window.__stamp === "s7fq2k"; // true only if the document was restored
Two more signals confirm it. A pageshow listener on the original page fires again with event.persisted === true, and Chrome will tell you why a restore did not happen if you are listening on the DevTools protocol:
await page.cdp("Page.enable");
// ...navigate, go back...
const ev = await page.events();
ev.filter((e) => e.method === "Page.backForwardCacheNotUsed")
.flatMap((e) => e.params.notRestoredExplanations.map((x) => x.reason));
There is one trap in this, and it cost me the first ten minutes. The obvious check looks like this:
performance.getEntriesByType("navigation")[0].type === "back_forward";
That is backwards. On a real bfcache restore no new navigation timing entry is created at all, because no navigation happened in the document: the entry you read is still the original one, and it still says navigate. Seeing back_forward means the browser threw your document away and built a new one from history. The string that sounds like proof of a restore is proof of the opposite.
11 of 12 on the public site
Four pairs of public pages, three runs each, pressing Back after every second load. Timings are from the call to history.back() until document.readyState is complete again:
| Journey | Restored | Back took |
|---|---|---|
| /guides to /about | 3 of 3 | 18, 22, 81ms |
| /about to /pricing | 3 of 3 | 12, 20, 24ms |
| /pricing to /about | 3 of 3 | 13, 24, 32ms |
| / to /guides | 2 of 3 | 11, 24ms, then 660ms |
Eleven restores out of twelve, most of them inside 25ms. You can watch this yourself: open the guides index, click through to a glossary entry, press Back, and notice there is no flash of a loading state because there is no load.
The one failure is the interesting row, and the protocol named its reasons:
MainResourceHasCacheControlNoStore
JsNetworkRequestReceivedCacheControlNoStoreResource
Two of our 76 public pages send no-store
Every page on the site is rendered by the same framework and served by the same host, but the cache headers are not the same:
$ curl -sI https://nakodo.app/guides | grep -i cache
cache-control: public, max-age=0, must-revalidate
x-vercel-cache: HIT
$ curl -sI https://nakodo.app/pricing | grep -i cache
cache-control: private, no-cache, no-store, max-age=0, must-revalidate
x-vercel-cache: MISS
The home page and the pricing page are the two public pages that render per request, because the header on them decides whether to say "Start free" or "Open the app" and that decision reads a cookie. Everything else is prerendered and cached.
Chrome's current behaviour for a no-store document is not a flat refusal. The page can enter the cache and then be evicted, which is why the same journey restored twice and failed once. The eviction trigger in that run was the second reason in the list: while the document sat in the cache, a script-initiated request came back with no-store on it. On this site that is the router prefetching one of those same two uncached pages. A link to the pricing page scrolling into view is enough.
So on the public side the rule is: 74 pages are cacheable and restore, and the two that read a cookie are the two that can be thrown out of memory, including when something else on the page asks for them.
0 of 6 behind the login
Inside the app, every private page sends no-store, and here the result was not probabilistic at all:
| Journey | Restored | Back took | Reasons |
|---|---|---|---|
| /campaigns to /settings | 0 of 3 | 354, 464, 957ms | all three |
| /settings/plan to /campaigns | 0 of 3 | 381, 444, 493ms | all three |
Six attempts, six rebuilds, every one reporting the same trio:
MainResourceHasCacheControlNoStore
JsNetworkRequestReceivedCacheControlNoStoreResource
BrowsingInstanceNotSwapped
Back inside the app therefore costs between a third of a second and a second, against about 20ms on the marketing site. It is the same browser, the same bundle and the same machine. The only difference is a header that is correct: a page listing one customer's campaigns has no business sitting in a shared disk cache, and no-store is how you say so.
Our own code is not the problem, which I had assumed it was
Before measuring I expected to find the blocker in our code, because a few screens in the app warn you about unsaved work. There are four of them, and they all look like this:
// Leaving with unsaved edits asks first.
useEffect(() => {
if (saved) return;
const warn = (e: BeforeUnloadEvent) => e.preventDefault();
window.addEventListener("beforeunload", warn);
return () => window.removeEventListener("beforeunload", warn);
}, [saved]);
Two things make that innocent. The first is the early return: the listener only exists while there is something to lose, so a page you have read and not typed into has no handler attached at all. The second is that beforeunload has not blocked bfcache in Chrome for years. The listener that does block it is unload, and a search of the whole app finds zero of those:
$ grep -rn "\"unload\"\|'unload'\|pagehide\|pageshow" src | wc -l
0
Zero unload, zero pagehide, zero pageshow. Nothing we wrote is keeping a document out of the cache, and nothing we could delete would put the app's pages in it.
What came out of it
-
The app's pages are not fixable without lying. Private, per-user HTML should carry
no-store. The honest improvement is to make Back cheaper rather than free: the lists in the app already keep their filters in the URL, so a rebuilt page comes back to the same view, and the work worth doing is in what that page has to fetch again. - The cookie read on two pages has a second cost. It was already the reason those two pages miss the CDN. It is now also the reason they can be evicted from memory, and the reason a prefetch of them can evict a page that was behaving. One cookie read, three separate prices.
-
Nothing on the public site listens for a restore. A restored document is a frozen one: whatever it said about counts or times is what you come back to. We have no
pageshowhandler anywhere, which is fine today because nothing on a public page ages within a browsing session, and is exactly the sort of fine that stops being fine the first time a public page shows a live number.
All of this is reproducible on the live site. Open nakodo.app/guides, run window.__x = 1 in the console, click into the glossary, press Back, and check window.__x. Then do the same starting from the pricing page a few times in a row, and watch the number come back some of the time and not others.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.