Use speculation rules prefetch/prerender when available #126
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.
Speculation rules prefetch, currently only available in Chromium-based browsers, has some advantages over
<link rel=prefetch>
:It stores the resources in a per-document in-memory cache, instead of in the HTTP cache, which can be slightly faster.
Because it has its own cache, it is not disabled by HTTP caching headers like Vary or Cache-Control.
It is automatically integrated into various user-respectful browser settings like Data Saver, Battery Saver, and memory pressure monitoring.
It has better cross-site support than
<link rel=prefetch>
, including the nonstandard as=document variant, because it disables itself if the destination site has cookies, whereas<link rel=prefetch>
can cache the wrong version of the document (the version without cookies).It shows up nicely in the DevTools speculative loads panel.
The implementation strategy is to just insert a
<script type=speculationrules>
element, with a single list rule pointing to the target URL, instead of inserting the corresponding<link rel=prefetch>
element.Additionally, we add the ability to configure prerendering, instead of prerendering, via the
data-instant-specrules=prerender
attribute. (Use of speculation rules can also be turned off, viadata-instant-specrules=no
.) Prerendering is more complex and risky, but can give a significant speed boost.More information on speculation rules prefetch and prerender is available at https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API.
When speculation rules are not available, we fall back to
<link rel=prefetch>
as usual.