Dev.to WebDev 🛠 Dev 👁 0 📖 6 min read

Back from an outbound link costs 70ms on 35 of our pages, and a full reload on the three that show a price

Notifio's site is 38 pages about a desktop app that watches rental search pages. Those pages carry 52 links that leave the site, nearly all of them citations on the comparison and guide pages, so "click a source, read it

Notifio's site is 38 pages about a desktop app that watches rental search pages. Those pages carry 52 links that leave the site, nearly all of them citations on the comparison and guide pages, so "click a source, read it, press Back" is a path a real reader takes.

I had never measured what Back costs. Chrome's back/forward cache can restore a page as a live object, JavaScript heap and scroll position included, and the interesting part is not whether it works in theory but which of your own pages are eligible.

The measurement

For each of the 38 URLs in our sitemap: load it, scroll to 1,500 pixels, navigate to a different origin, press Back, then ask the page whether it is the same page or a new one.

The reliable detector is a token written at load time plus the pageshow event, which fires with persisted: true only on a restore:

// injected before any page script runs
window.__token = Math.random().toString(36).slice(2);
window.__ps = [];
addEventListener('pageshow', e => window.__ps.push({ persisted: e.persisted, wall: Date.now() }));

If the token survives and a persisted pageshow arrived, the document was restored rather than rebuilt. Chrome will also tell you why a restore did not happen, over the DevTools Protocol:

const cdp = await ctx.newCDPSession(page);
await cdp.send('Page.enable');
cdp.on('Page.backForwardCacheNotUsed', e =>
  console.log(e.notRestoredExplanations.map(x => x.reason)));

Two instrument bugs first, because they both pointed the wrong way

My first two runs said 0 of 38 pages were eligible.

Run one navigated from the page under test to another page on the same site, which produced one reason on all 38:

notUsed=["BrowsingInstanceNotSwapped"]

That is an artifact of driving a same-site navigation this way, and it is also the wrong scenario: an in-site click on this site is a client-side route change rather than a navigation at all. Leaving for a genuinely different origin is both the honest test and the thing readers do when they click a citation.

Run two left for example.com and produced four reasons:

notUsed=["BackForwardCacheDisabled","MainResourceHasCacheControlNoStore",
         "JsNetworkRequestReceivedCacheControlNoStoreResource",
         "BackForwardCacheDisabledByCommandLine","UnloadHandlerExistsInMainFrame"]

Two of those are my fault.

BackForwardCacheDisabledByCommandLine is Playwright. It launches Chromium with --disable-back-forward-cache as part of its default argument list, which is the right call for a test runner and fatal for this measurement. You have to put it back:

chromium.launch({ executablePath: EXEC, ignoreDefaultArgs: ['--disable-back-forward-cache'] })

UnloadHandlerExistsInMainFrame is worse, because it was me. My injected probe contained a one-line addEventListener('unload', () => {}) left over from an earlier idea, and an unload handler makes a page ineligible by itself. I spent a while believing our site had an unload handler before I read my own script.

With the unload handler deleted and the command-line flag put back, the numbers are clean, and only one of the original reasons survives.

35 of 38

Pages Restored What Chrome says
35 yes nothing, it just restores
3 no MainResourceHasCacheControlNoStore

The three are /, /pricing and /download, and one curl -I explains it:

$ curl -sI https://notifio.app/help      | grep -i cache-control
cache-control: public, max-age=0, must-revalidate

$ curl -sI https://notifio.app/pricing   | grep -i cache-control
cache-control: private, no-cache, no-store, max-age=0, must-revalidate

Those three pages are the three that render per request, and the only thing that makes them dynamic is that a price is on screen: the visitor's currency is resolved on the server, so the page cannot be cached and Next.js says so in the header. Chrome reads no-store as "this response may contain something private" and refuses to keep the document alive in memory after you leave.

So the rule on this site turns out to be legible: pages that quote money cannot be restored, pages that only describe the product can.

What a restore actually costs

Measuring the latency properly means timestamping the pageshow in the page and comparing it with the clock in the driving script, since both run on the same machine:

Page Restored Latency Requests during Back Scroll
/help yes 72 ms 2 1500 of 1500
/terms yes 70 ms 2 1500
/privacy yes 72 ms 2 1500
/alerts/kamernet yes 70 ms 2 1500
/compare/rentbird yes 67 ms 2 1500
/guides/rental-scams-netherlands yes 71 ms 2 1500
/ no n/a 27 1164
/pricing no n/a 71 1370
/download no n/a 71 1057

Three details in that table are worth more than the headline.

The same JavaScript context comes back. The random token matches, and the DOM node count is identical to the byte. Nothing re-renders, nothing re-hydrates, no React tree is rebuilt.

A perfect restore still makes two requests. Every single one of them is the same pair:

GET https://notifio.app/icon.png
GET https://notifio.app/icon-light.svg

The favicon and the logo, re-fetched on restore, and that is Chrome rather than our code. I like this detail because "zero network" is the claim people make about the back/forward cache, and the honest number on this site is two requests and no HTML.

The scroll position is the real difference. On a restored page you land at 1500 because you never left. On the three that reload, Chrome's scroll restoration has to guess during a fresh render, and it lands at 1164, 1370 and 1057. Our homepage is tall, so being put 336 pixels above where you were reads as "the page lost my place", which is a worse experience than the 400 extra milliseconds it took to get there.

One oddity: /about restored correctly but made 41 requests during the restore rather than 2. It is the one restored page where the node count changed, from 158 to 159. I have not chased it down, and I am noting it rather than smoothing it over.

The counter-intuitive bit about navigation timing

A restored page reports the wrong navigation type, or rather the honest one:

performance.getEntriesByType('navigation')[0].type
// restored page:     "navigate"
// reloaded page:     "back_forward"

The pages that come back from the cache still say navigate, because the navigation entry belongs to the original load and no second navigation ever happened. The only pages reporting back_forward are the three that failed to be restored. If you are tempted to measure bfcache usage in the field by counting back_forward navigation types, you will count exactly the cases where it did not work.

What I am changing

Nothing, which surprised me.

The three ineligible pages are ineligible because they personalise a price per request, and the figure on the page is always the figure charged. Trading that for a faster Back on three of 38 pages is the wrong way round. The fix that would make them eligible, rendering a generic page and swapping the price in on the client, buys a 70 millisecond restore and costs a visible flash of the wrong currency.

What I would change if the numbers had come out differently: a reader pressing Back should be the cheapest thing a site does, and if those 35 pages had failed on something incidental, a stray unload handler or a third-party script, it would have been worth a commit. The whole point of measuring was to find out which kind of problem I had.

If you want to run this on your own site, you do not need any of the code above. Open a page, navigate away, press Back, and read the "Back/forward cache" panel in DevTools, which names the same reasons the protocol does. The Playwright version is only worth writing when you want all 38 answers in one go.

Notifio is at notifio.app, it is a one-off payment, and what it does once installed is written up here.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.