-
Notifications
You must be signed in to change notification settings - Fork 436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache snapshots synchronously #80
Conversation
Snapshot caches have been stored asynchronously since 0bd5e78 (May 2018). The recent change just converts this from a setTimeout callback to a promise. Note that the snapshot itself is captured synchronously. It's the process of saving it that's async. We do this so other scripts on the page have a chance to tear down the page before it goes into cache. Full story here: turbolinks/turbolinks#390 |
Thanks! As you noticed, I'm a little bit in over my head, but I hope you can indulge me, because I can still consistently reproduce this problem and I've investigated a bit further. I did notice just now that it ususally surfaces when I navigate to a page that has a Youtube/Vimeo embed. The page that I navigated away from is then cached with the permanent element replaced by a From what I now understand, the problem could be the logic of the loadResponse() {
if (this.response) {
const {statusCode: statusCode, responseHTML: responseHTML} = this.response;
this.render((() => {
this.cacheSnapshot(); <--- this is ASYNC
if (isSuccessful(statusCode) && responseHTML != null) {
this.view.render({
snapshot: Snapshot.fromHTMLString(responseHTML)
}, this.performScroll);
...
}
}));
}
} (The same logic is present in That the timing is left up to chance (to some extent) seems to be confirmed by some call logging in the console---in my use case
From experimenting further, it appears that removing Let me know if I can/should convert this comment to an open issue, or change the PR. |
@sstephenson Now that I understand it better I can see that you were already ahead of me with regards to the diagnosis. However, I can't find any code that guarantees that the permanent elements are in the DOM when the snapshot is cached. No matter how I look at it, I always come back to that |
@stgm Would you mind opening a separate issue with as specific of a diagnosis as you're able to make? |
Just did, after reading up about the not-so-asyncness of JS :-) |
This is a bit of a cheeky way to report a bug, but this change works for me, so I thought I'd propose it.
I have a
div
marked asdata-turbo-permanent
. The contents of thisdiv
are not changed at any time when testing, it's just marked as permanent. Thediv
is not contained inside aturbo-frame
.On my M1 Mac, I've got a problem in Safari. When clicking a link to visit a new page, the
div
is correctly cached and restored at all times. However, when going back/forth in the browser, more often than not, some of the pages will lose the sidebar contents and have the placeholdermeta
tag instead. From that point on, such a page will never have the sidebar content again. Firefox doesn't show this problem.From some logging, it appears that in some cases, the snapshot is cached right after moving the permanent elements out of the DOM. Hence, removing the
async
flag fromView.cacheSnapshot()
is one way to resolve this problem.Now this
async
flag is new in Turbo---in Turbolinks, the analogous part wasn't marked asasync
and I can confirm that I had never previously encountered this problem. This might indicate that it is indeed the culprit?