-
Notifications
You must be signed in to change notification settings - Fork 621
Turbolinks should save page state before it is closed #185
Comments
I am running into the need for the as well. Without this feature, its too easy to cache pages in an unwanted state and impossible to get a fresh rebuild from the server. |
@domchristie the problem is that the page could be modified before it is unloaded while turbolinks keeps the old version of the page, created after the page was loaded. In this case when you click back you see an outdated page, different than it actually was before you left it. |
Turbolinks caches a page just before a new one is rendered (rather than just after it has loaded) … but I have just noticed that the end of your original post, you mention "where turbolinks is disabled", in which case I think pressing "Back" will fallback back to a browser's default behaviour:
|
You can see yourself the different behavior even here, on GitHub. I just wrote this comment but didn't submit it. Instead, I clicked on the Then two different scenarios are possible:
I hope this will give you more understanding of my point of view. Since it would be a really bad design keeping track of all page changes and modifying the cache on the go, especially on dynamic pages, using |
In this case, the cache is handled by the browser according to the spec—storing the page state when it was initially retrieved. The Turbolinks cache is empty because the page was unloaded. The reason the text box value is restored is because GitHub stores input values in session storage before exiting the page (possibly on
The problem is that when you fully load a page without Turbolinks (e.g. via a link with It's technically possible to store the state of the page in session storage, but aside from the security concerns, it would be difficult to know when to restore from this cache, and it'd also be prone to visual flicker when Turbolinks takes over following the default browser "Back" behaviour (which is unavoidable in this case). So all things considered, it is best to keep as many Turbolinks-enabled transitions as possible in your app! :) Hope that makes sense! Using session storage to cache pages for offline is being discussed in #196, so I'm closing this. |
It's not GitHub storing anything on the unload event, you can test here https://www.w3schools.com/html/html_forms.asp (just enter any value in the form then close and re-open the page). Google Chrome and Safari restored my custom value in both cases: back/forth navigation and closing/re-opening the tab. Firefox did only if I hit back/forth but didn't if I completely close and re-open the tab. A web form was just an example, the page could be modified with other scripts and the native back button restores them properly, while Turbolinks breaks this. I don't use Turbolinks now and don't have a place where to test but doesn't it already flicker replacing the restored by the browser (most recent) page from a stale cache? I think that was the original issue for this report. I'm glad you were able to convince yourself this is not an issue with Turbolinks. |
Another issue with GitHub – posted my last comment and then clicked this link https://www.w3schools.com/html/html_forms.asp, then clicked back and my post disappeared due to the stale cache. UPD. I think I just remembered our use case – we had an infinite scroll and each time user clicked the link and then wanted to return back to the same place. So a workaround might be to add a special data attribute to the link to allow it to save the current state before exit. Which will not resolve the problem with restored tabs, though. |
To elaborate, GitHub does store form values in session storage: Perhaps because this kind of caching behaviour is not strictly defined, as well as for cross-browser consistency.
This isn't the case. When transitioning between Turbolinks-enabled pages, Turbolinks caches the page just before the next page is rendered. To demonstrate this, type a new comment, tap "Preview", scroll up and tap "Pull Requests", then navigate "Back". The preview tab will still be active.
GitHub/Turbolinks has no control over what the browser initially serves when navigating "Back" from |
The page could be modified after the last turbolinks event, e.g. some elements could be added/hidden, input values might be changed, etc. I think it would be correct to bind to
beforeunload
event and update the cache before closing the tab/following link where turbolinks is disabled.The text was updated successfully, but these errors were encountered: