This plugin adds support for the Speculation Rules API, which allows defining rules by which certain URLs are dynamically prefetched or prerendered based on user interaction.
Install the plugin from npm:
npm install eleventy-plugin-speculation-rules --save-dev
mode - The speculation mode.prerender, options: prefetch, prerendereagerness - The eagerness level for speculation.moderate, options: conservative, moderate, eagernoPrerenderSelector - CSS selector for links that should not be prerendered..no-prerenderinclude - Array of URL patterns to include.[]exclude - Array of URL patterns to exclude.[]By default, the plugin excludes .zip and .pdf files, as well as links with rel="nofollow".
Add it to your Eleventy Config file:
import eleventyPluginSpeculationRules from 'eleventy-plugin-speculation-rules';
export default function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginSpeculationRules);
};
const eleventyPluginSpeculationRules = require('eleventy-plugin-speculation-rules');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginSpeculationRules);
};
import eleventyPluginSpeculationRules from 'eleventy-plugin-speculation-rules';
export default function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyPluginSpeculationRules, {
mode: 'prefetch',
eagerness: 'eager',
noPrerenderSelector: '.no-prerender',
include: ['/blog/*'],
exclude: ['/admin/*']
});
};
prefetch - The browser will prefetch the URL when the user hovers over the link.prerender - The browser will prerender the URL when the user hovers over the link.Prerendering will lead to faster load times than prefetching. However, in case of interactive content, prefetching may be a safer choice.
conservative - Speculates only on clickmoderate - Speculates on hovereager - Speculates on slightest indicationThe eagerness setting defines the heuristics based on which the loading is triggered. "Eager" will have the minimum delay to start speculative loads, "Conservative" increases the chance that only URLs the user actually navigates to are loaded.
Check the network tab in your browser's developer tools to see if the page is prefetched or prerendered.
More information about the Speculation Rules API can be found here.
More about Debugging speculation rules.
The Speculation Rules API is a new web API, and the functionality used by the plugin is supported in Chromium-based browsers such as Chrome, Edge, or Opera using version 121 or above. Other browsers such as Safari and Firefox will ignore the functionality with no ill effects but will not benefit from the speculative loading. Note that extensions may disable preloading by default (for example, uBlock Origin does this).