Record and cancel pending prefetch requests #1490
Open
+97
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1244, #1372, #1479
Inspired by #1244 (comment) by @SHPmax
What's this?
Turbo can speed up page loads by prefetching requests. If the connection is slow or the server takes some time to respond, this currently has two side-effects, though:
Requesting a resource (that takes longer to load than the prefetch delay) multiple times - e.g. by quickly hovering back and forth over a link - will result in multiple requests to this same resource, that are running in parallel.
fetches-piling-up.mp4
If you request a resource that loads slow, then one that loads fast, the promise of the fast one will fulfill first (displaying a page for instance), then the slow one - that is still running - kicks in and overwrites the result.
2025-12-13.15-03-52.mp4
(taken from Prefetch causes wrong pages to be rendered #1479)
Solution
This PR makes the
link_prefetch_observerrecord the last fetch request and cancel the previous one to the same URL. On top of that, duringturbo:visit, all currently recorded requests but the one for the current URL are cancelled as well. This way, there now can only be one active prefetch request per URL and never a pending one of a different URL a visit happens.I added test cases to outline the issue. You can see this PR in effect by commenting out the two added
fetchRequest.cancel()statements in thelink_prefetch_observer./cc @fritzmg, @ilyadh