instantpage / instant.page

Make your site’s pages instant in 1 minute and improve your conversion rate by 1%
https://instant.page
MIT License
6.01k stars 206 forks source link

Use speculation rules prefetch/prerender when available #126

Closed domenic closed 4 months ago

domenic commented 4 months ago

Speculation rules prefetch, currently only available in Chromium-based browsers, has some advantages over <link rel=prefetch>:

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, via data-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.

kurtextrem commented 4 months ago

lgtm although it looks like the implementation doesn't allow cancelling (by removing the script tag, which might mean we run into max prefetches sooner compared to link rel=prefetch)?

domenic commented 4 months ago

Right, I didn't see that ability with <link rel=prefetch>, so I didn't add it here. But should we consider adding it somehow? We could manage our own FIFO queue maybe, and remove the oldest one when we get near a limit?

dieulot commented 4 months ago

Thanks for your great PR!

Release-wise, my hope is that I can deliver a major version incorporating it soon enough. If that isn’t done by mid-April, I’ll release a minor version just for it.

Regarding the ability to cancel a request, it was removed way back in 2019 with 3.0.0, mostly because it made the code easier. (It should eventually come back.)