-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Astro's prefetch
doesn't work in non-Chromium-based browsers
#10464
Comments
I used Safari exclusively when implementing this, so it should work. Are you testing this in dev or prod? The prefetch behaviour also relies on a proper IIRC Chrome implements some unique heuristics, e.g. checking date of request and guessing a proper cache time if If you're unsure if the prefetching is actually done by the script, you can enable debug/verbose logging in the browser console to see the prefetch logs (only in dev). |
Hi @bluwy and thanks for your reply. So, I've copied the StackBlitz repo linked above to GitHub and deployed it to Cloudflare. The result doesn't change. Instead of Chrome I am using Edge but the behavior on Chrome is the same. Sorry for the UI in Italian but it seems I cannot change it easily. Edge / ChromeIf I refresh the about page, you can see contacts and index fetched in the network tab: Then by navigating to contacts it's clear that it navigates to a prefetched page: SafariIf I refresh the about page that's what I see in the network tab (no prefetched pages): If I navigate to contacts, it's clear that that page has been downloaded as it took 216ms ("scarica" means "downloaded") and doesn't come from the prefetch cache: On Firefox I have the same identical behavior of Safari. I have published 4 Astro websites in the last few months and they are all affetcted by the same prefetch issue hence I guess it's not just my device or the dev environment. Thank you again for your reply. |
Thanks! That's very helpful to understand the current situation. It looks like:
I'm not exactly sure how to make these bullet-proof yet, but this is interesting to think about. |
Great, thanks for providing additional insights! If I may add another couple of things I noticed: Calling <script>
import { prefetch } from 'astro:prefetch'
document.addEventListener('astro:page-load', () => {
prefetch('/', { with: 'fetch' })
prefetch('/about', { with: 'fetch' })
prefetch('/contact', { with: 'fetch' })
})
</script> I can then see the pages downloaded in the network tab on both browsers: But once I try to navigate to one of them, they're downloaded again: I've added a branch to the repo linked above and deployed it Cloudflare just in case. Cheers! |
@bluwy is this a bug? |
Yeah, it should work for Safari and Firefox. I think we need to figure out a support path for it, otherwise we need to document the caveats. |
See also #10907 |
Sure we can!
I'll investigate it further and probably open a PR if we agree on this solution. |
@V3RON I was just working on this today and I noticed that 😄 I'll be sharing my findings here shortly and open a PR to restructure things, so a PR won't be needed for now, but thanks! |
I ran some tests locally to see how this all works across different browsers, and how Chrome
Firefox
Safari
Notes about cache headers
SummaryTo re-iterate, if all pages have cache headers ( Next steps
|
Astro Info
If this issue only occurs in one browser, which browser is a problem?
Firefox, Safari
Describe the Bug
According to the docs, Astro prefetch should work on any browser whether or not View Transitions are enabled (as it is not mentioned otherwise).
I virtually tried any combination under my
astro.config.mjs
:prefetch: true
,prefetch.prefetchAll: true
, etc. And also to manually append thedata-astro-prefetch
attribute as explained in the docs and enable/disable ViewTransitions.Under the "Network" tab of both Firefox 124 (latest) and Safari 17.4 (latest) there is no way I can see the incoming documents.
The only way to see them in that tab is to manually call
prefetch()
in a script tag using{ with: 'fetch' }
, however, those pages are then requested again to the server once I click on the correspondent links.On Chrome and Edge (and I suppose any other Chromium-based browser) everything works as per docs.
I am not sure if this expected, if it's a bug or if I (hopefully) misinterpreted the docs and actually doing something wrong. Thank you.
What's the expected result?
To work on Firefox and Safari as it works on Chrome.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/withastro-astro-rdn9rn?file=src%2Flayouts%2FLayout.astro
Participation
The text was updated successfully, but these errors were encountered: