DEV Community

Vladimir Elchinov for Session Replay

Posted on

"The Cart Showed the Old Total." The Back Button Restored a Snapshot, and Your Load Code Never Ran.

A customer writes in: they changed the quantity in their cart, went back to the product page, and the cart icon in the header still showed the old total. You open the product page, check the header, and it is right. You reload it, and it is right. You cannot make it wrong.

You are not following their steps. They pressed the back button, and you typed the address or reloaded. In every major browser those are two different kinds of navigation, and only one of them runs your page's code again.

What "back" actually does now

All major browsers have a back/forward cache. Instead of throwing a page away when the user leaves it, the browser keeps it in memory, and if they come back, it hands them the page exactly as they left it. web.dev's description is precise about how much is kept: the bfcache "is a snapshot of the entire page in memory, including the JavaScript heap".

The whole heap. Not the HTML re-parsed, not your scripts re-run: the same objects, the same variables, the same DOM, frozen when the user left and thawed when they returned. That is why it is fast, and it is why your header still says what it said before they changed the cart on another page.

The code you wrote to fill in the header runs on DOMContentLoaded or load, or in a framework's mount hook. None of that runs on a restore, because nothing is loading. The page was never gone.

Why you cannot reproduce it

Every way a developer normally revisits a page skips the cache. Reloading loads it fresh. Typing the URL is a new navigation. Opening it in a new tab is a new page. Clicking a link to it is a new page. The only path that restores a snapshot is the one users take constantly and developers rarely do while debugging: the back and forward buttons.

And it is not even every time. Whether a page goes into the cache depends on what it is doing when the user leaves, and web.dev's guidance lists the things that get in the way, starting with the most important: "never use the unload event". So the same page can be cached on one visit and not on the next, depending on what had happened in it. Some users see the stale header, some do not, and nothing in your logs distinguishes them, because a restore makes no request at all.

The event that does fire

There is one event that runs on a restore, pageshow, and it tells you which kind of arrival this is. In web.dev's words, its persisted property "is true if the page was restored from bfcache and false otherwise".

So anything that can go stale while the user is on another page needs refreshing there:

window.addEventListener("pageshow", (event) => {
  if (event.persisted) {
    // Restored from memory: nothing else is going to run.
    refreshCartBadge();
    refreshSignedInState();
  }
});
Enter fullscreen mode Exit fullscreen mode

web.dev's own examples are the two that matter most. A cart changed on a later page shows "out-of-date information" when the earlier page comes back. And the more serious one: a user who signs out on a shared computer, after which the next person presses back and gets the signed-in page as it was.

The fix is not to make the page uncacheable. That throws away the fastest navigation the browser has for every user, to protect a handful of values you can simply refresh on pageshow.

How to see it for yourself

Chrome DevTools has a panel for exactly this: Application, then Back-forward Cache, then Run Test. It navigates away and back for you and reports whether the page was "Restored from back-forward cache", and if not, what prevented it.

Then reproduce the report the way the user did. Not reload, not a new tab: go to the other page, change the thing, and press back.

What the report was missing

"The cart showed the old total" is a true report of something that cannot happen on any page load you will ever trigger. The single fact that explains it, that they got there with the back button, is the one thing nobody thinks to mention, because to the person using the site, back is not a special way of arriving anywhere. It is just how you get back.

Top comments (0)