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

fix: client prerender fallback #10295

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/warm-spoons-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"astro": patch
---

Prerendering with the Speculation Rules API can fail---for example, [certain Chrome extensions can block prerendering](https://developer.chrome.com/blog/speculation-rules-improvements#chrome-limits). Currently in this case, neither prerendering or prefetching occurs.

Adding `prefetch` in addition to `prerender` within the speculation rules script does not create an extra request. This change will allow browsers to fallback to prefetching if prerendering is not supported.
bluwy marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ export interface AstroUserConfig {
* @description
* Enables pre-rendering your prefetched pages on the client in supported browsers.
*
* This feature uses the experimental [Speculation Rules Web API](https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API) and overrides the default `prefetch` behavior globally to prerender links on the client.
* This feature uses the experimental [Speculation Rules Web API](https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API) and enhances the default `prefetch` behavior globally to prerender links on the client.
* You may wish to review the [possible risks when prerendering on the client](https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API#unsafe_prefetching) before enabling this feature.
*
* Enable client side prerendering in your `astro.config.mjs` along with any desired `prefetch` configuration options:
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/src/prefetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ function appendSpeculationRules(url: string) {
urls: [url],
},
],
// Currently, adding `prefetch` is required to fallback if `prerender` fails.
// Possibly will be automatic in the future, in which case it can be removed.
// https://github.com/WICG/nav-speculation/issues/162#issuecomment-1977818473
prefetch: [
{
source: 'list',
urls: [url],
},
],
});
document.head.append(script);
}
Loading