-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Update prefetch docs with browser support information #8246
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d5181f
Update prefetch docs with browser support information
bluwy 36a0588
Apply suggestions from code review
bluwy f9bfc0a
Update docs
bluwy e04fef1
Merge branch 'main' into prefetch-docs-update
bluwy 247a580
replacing "should"
sarah11918 a1fe157
Merge branch 'main' into prefetch-docs-update
sarah11918 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,6 @@ Each strategy is fine-tuned to only prefetch when needed and save your users' ba | |
|
||
- If a visitor is using [data saver mode](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData) or has a [slow connection](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType), prefetch will fallback to the `tap` strategy. | ||
- Quickly hovering or scrolling over links will not prefetch them. | ||
- Links that use the `viewport` or `load` strategy are prefetched with a lower priority to avoid clogging up the network. | ||
|
||
### Default prefetch strategy | ||
|
||
|
@@ -106,17 +105,6 @@ As some navigation might not always appear as `<a />` links, you can also prefet | |
</script> | ||
``` | ||
|
||
You can additionally configure the priority of prefetching by passing the `with` option: | ||
|
||
```js | ||
// Prefetch with `fetch()`, which has a higher priority. | ||
prefetch('/about', { with: 'fetch' }); | ||
|
||
// Prefetch with `<link rel="prefetch">`, which has a lower priority | ||
// and manually scheduled by the browser. (default) | ||
prefetch('/about', { with: 'link' }); | ||
``` | ||
Comment on lines
-109
to
-118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
The `prefetch()` API includes the same [data saver mode](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData) and [slow connection](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType) detection so that it only prefetches when needed. | ||
|
||
To ignore slow connection detection, you can use the `ignoreSlowConnection` option: | ||
|
@@ -154,6 +142,38 @@ export default defineConfig({ | |
}); | ||
``` | ||
|
||
## Browser support | ||
|
||
Astro's prefetching uses [`<link rel="prefetch">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch) if supported by the browser, and falls back to the [`fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) otherwise. | ||
|
||
The most common browsers support Astro's prefetching with subtle differences: | ||
|
||
### Chrome | ||
|
||
Chrome supports `<link rel="prefetch">`. Prefetching works as intended. | ||
|
||
### Firefox | ||
|
||
Firefox supports `<link rel="prefetch">` but may display errors or fail entirely: | ||
|
||
- Without an explicit cache header (e.g. [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) or [`Expires`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires)), prefetching will error with `NS_BINDING_ABORTED`. | ||
- Even in the event of an error, if the response has a proper [`ETag`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) header, it will be re-used on navigation. | ||
- Otherwise, if it errors with no other cache headers, the prefetch will not work. | ||
|
||
### Safari | ||
|
||
Safari does not support `<link rel="prefetch">` and will fall back to the `fetch()` API which requires cache headers (e.g. [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control), [`Expires`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires), and [`ETag`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)) to be set. Otherwise, the prefetch will not work. | ||
|
||
**Edge case:** `ETag` headers do not work in private windows. | ||
|
||
### Recommendations | ||
|
||
To best support all browsers, make sure your pages have the proper cache headers. | ||
|
||
For static or prerendered pages, the `ETag` header is often automatically set by the deployment platform and is expected to work out of the box. | ||
|
||
For dynamic and server-side rendered pages, set the appropriate cache headers yourself based on the page content. Visit the [MDN documentation on HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) for more information. | ||
|
||
## Migrating from `@astrojs/prefetch` | ||
|
||
The `@astrojs/prefetch` integration was deprecated in v3.5.0 and will eventually be removed entirely. Use the following instructions to migrate to Astro's built-in prefetching which replaces this integration. | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will no longer be the case as all strategies will internally use
<link rel="prefetch">
which has the same (low) priority.