Record and cancel pending prefetch requests#1490
Conversation
|
I found a way of further improving this. I will update the PR accordingly. |
f22f7b3 to
44a5764
Compare
…l being used for a visit
|
Just to illustrate the difference. This is the behavior with this PR: optimized-prefetch.mp4
|
fritzmg
left a comment
There was a problem hiding this comment.
The built resources are also deployed on https://www.inspiredminds.at/turbo.php - and I can confirm that this fixes #1479 and I could not find any other issues 👍
|
Is there anything I could do to move this forward? /cc @packagethief maybe? |
|
Thank you @m-vo for the fix; can't wait for it to be merged |
|
I've had the same issue happen to me on GitHub quite a bunch of times when my network was slow. It's just more obvious when knowing that it's a possible bug in the Turbo library and does not happen on faster connections / good optimized pages as often. |
|
@packagethief @jorgemanrubia would you be open to merging this PR? Or should this stay an opt-in solution that users implement themselves when needed, as outlined in #1244 (comment)? |
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