Continuing the discussion from Disabling automatic scroll restoration on navigation:
As a user, I want better platform-wide base support for things like caching page content/state and saving/restoring form state across history traversal (and whether restoring state should happen before or after the scripts on the page first synchronously run, to allow JS to calculate defaults without having to care if this has happened).
For example, right now on GitHub, if I visit an issue’s thread and stay on the page until a new comment comes in, that new comment is added to the thread in real-time. However, if I navigate away from the page and use Back to return to it, those comments disappear.
This could maybe be solved with an application-specific use of the state object with
replaceState, but it would make just as much sense to have a mechanism by which a page can say “the current DOM content is the new cached version of the page, use it to replace the content currently in the cache”. (Even with Service Workers, this kind of cache-replacement is non-trivial: I guess you could override the cache, do “document.html.outerHTML” and then make some kind of secret-handshake Fetch to inform the service to use it, but now you’ve killed UA caching, especially UA cache eviction, just to implement a trivially different version.)
As an example of the interaction of form behavior with history, when I navigate away from a page where I’m filling something out (specifically, the “New issue” page, or the comment field on an existing issue), the form will be re-filled with what I had been typing… sometimes. I’m not sure if this is a UA feature or a GitHub feature, but it seems to differ between the New Issue page and the Add Comment form, and between.
This is another thing that could be solved with some mechanism to snapshot page state in history at a page-content level: it could also be given special treatment, where a page may request a behavior like “save the page content, but don’t save the form content”, or “save the form content, but not the page elements” (to prevent ephemeral things like dynamically-generated tooltips from being pegged as page content).
Declarativity and Default Behavior
lt would be nice if the default behaviors of the UA were made helpful-by-default for users (including mechanisms like declarative properties to control this behavior) and authors (developers), rather than requiring pages to opt-in to recovering form state (or, worse, right a hacky implementation to do it themselves).
Of course, there are potential issues in situations where a user on a shared computer navigates away from a page and expects sensitive form content to be cleared - however, this is already a problem to some extent, and the way the Web ecosystem is gravitating toward solving it is, rightly, to add browser-level feature support for shared computing, like incognito/private browsing, kiosk mode, and associating session / history to user accounts. Pages may opt to tighten their history if necessary for their specific case, but forcing this level of paranoia on all pages is not called for (the benefits of it working as intended outweigh the risks of it being exploited).