Skip to content
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

Ensure that page refreshes do not trigger a snapshot cache #1196

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

afcapel
Copy link
Collaborator

@afcapel afcapel commented Feb 21, 2024

Fixes a bug introduced in #1146

exemptPageFromPreview() adds a <meta> tag to the <head> setting turbo-cache-control to no-preview. However, since the MorphRenderer now inherits from the PageRenderer, it can update meta tags in the head and remove the turbo-cache-control tag. This means that the snapshot cache will be used for the next visit, which is not what we want.

Specifying shouldCacheSnapshot: false in the visit options ensures that the snapshot cache is not used for the refresh visit.

Fixes a bug introduced in #1146

`exemptPageFromPreview()` adds a `<meta>` tag to the `<head>` setting
`turbo-cache-control` to `no-preview`. However, since the MorphRenderer
now inherits from the PageRenderer, it can update meta tags in the head
and remove the `turbo-cache-control` tag. This means that the snapshot
cache will be used for the next visit, which is not what we want.

Specifying `shouldCacheSnapshot: false` in the `visit` options ensures
that the snapshot cache is not used for the refresh visit.
@pfeiffer
Copy link
Contributor

pfeiffer commented Feb 21, 2024

Hey, sorry if hijacking, but related to this is a few other issues and PRs (#1186, #1188) that I've been exploring around page refreshes, in particular how they behave in Turbo Native with Turbo 8.

Specifically refreshes in Turbo Native will make the native app unresponsive as the native apps will display a spinner and blank the WebView during the refresh action, more or less breaking any native apps that subscribe to refresh broadcasts:

MorphingShowingSpinners.mp4

I'm wondering what the reason is for exempting refreshes from the snapshot cache? I've explored actually making a snapshot of the page before a refresh in #1188 which significantly improves the Turbo Native experience, as at least the WebViews aren't blanked out while refreshing. This PR seems to re-iterate the wish to exempt refreshes from snapshots, but it's not clear to me why?

@afcapel
Copy link
Collaborator Author

afcapel commented Feb 21, 2024

@pfeiffer we've found a few issues in which rendering a cache snapshot interferes with the morphing algorithm. In particular we don't want a cached preview rendering on a morph refresh because that can override any changes to the current page, which is what we want to avoid with morphing.

For example, we found that rendering a cached snapshot may result in closing a modal that was added dynamically to the page after the cache snapshot was taken.

@afcapel afcapel merged commit cdd3079 into main Feb 21, 2024
2 checks passed
@afcapel afcapel deleted the dont-cache-refreshes branch February 21, 2024 16:12
@pfeiffer
Copy link
Contributor

pfeiffer commented Feb 21, 2024

In particular we don't want a cached preview rendering on a morph refresh because that can override any changes to the current page, which is what we want to avoid with morphing.

For example, we found that rendering a cached snapshot may result in closing a modal that was added dynamically to the page after the cache snapshot was taken.

Wouldn't that be solved by explicitly making a new snapshot before refreshing as proposed in #1188? In my tests, I've seen the issues you describe if there was snapshot of the page taken way earlier (ie. as part of a advance -> restore -> refresh cycle). In that case I've seen eg. a modal being closed during the refresh visit, as the snapshot was taken of the page while navigating with the advance action.

By explicitly taking a snapshot in #1188 before refreshes, we ensure that snapshots being shown are up to date with the current state (ie modals open).

@afcapel
Copy link
Collaborator Author

afcapel commented Feb 22, 2024

@pfeiffer that's good point, it may be an implementation artifact. I'll give #1188 a try, to see if it solves the problems we've seen, and will come back to you 👍

@afcapel
Copy link
Collaborator Author

afcapel commented Feb 22, 2024

@pfeiffer I've given #1188 a try but unfortunately it doesn't solve some the issues we've seen.

In our apps we have some code that prepares a page to be cached on a turbo:before-cache event. For example, we need to close modals before caching the page, so you don't see a modal flashing when you restore a cached page. If we trigger a cache on a page refresh, that will trigger the modal closing behavior too.

Conceptually, page caches should happen when you leave a page, so we have the most recent snapshot if you ever come back to that location. In a page refresh you don't leave the page and there's no need to take a snapshot.

But it's true that the blank page flickering is annoying and we should fix it. My preferred solution for this would be to fix hotwired/turbo-ios#136 in the native adapters, so they don't show a blank screen and a spinner when a visit targets the current page. That should make the solution independent of whether the page is cached or not and make it more robust.

//cc @jayohms

@jayohms
Copy link
Collaborator

jayohms commented Feb 22, 2024

It looks like the iOS-specific issue is due to the adapter.visitStarted() callback after a refresh is initiated and how the turbo-ios adapter works: https://github.com/hotwired/turbo-ios/blob/c9d060fcdec136c3808cd0764e956d375012c6fb/Source/Session/Session.swift#L173

I'll look at this separately, but we should be able to stop displaying the activity/progress indicator in the iOS library during same-page refreshes/visits.

@pfeiffer
Copy link
Contributor

I've opened hotwired/turbo-ios#177 which addresses the issue; specifically skipping proposing locations if the URLs are the same (as proposed in hotwired/turbo-ios#160 by @afcapel) and on the native side skipping showing screenshots and activity indicators if the visit is a page refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants