A web browser’s back button is more than a link to the previous page. It is the navigational equivalent of undo. Going back should restore the previous page, exactly as it was last time we were there. Conversely, the forward button is redo. These buttons traverse history — they should never mutate state.
Luckily, the browser is a capable time machine by default, at least for regular web sites.
It will cache content, remember form values, reset scroll bars, and all the other things required to preserve the browser history.
Just make sure to keep
Cache-Control: no-store out of your response headers.
However, for web apps, where we make our own little world of routes and state, it’s up to us to tackle time travel. And while an implementation as polished as the browser’s may be out of scope, a little consideration goes a long way.
An expired session is not an error when going backwards.
Navigating history should never clear data or result in a spinner.
It should not show any sudden
5xx errors because of a fetch.
Previous form values should be restored, even if invalid.
And navigating history should always work — even when offline.