Skip to content

An Eleventy 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.

License

Notifications You must be signed in to change notification settings

reatlat/eleventy-plugin-speculation-rules

Repository files navigation

eleventy-plugin-speculation-rules

npm npm license

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.

Demo: https://eleventy-plugin-speculation-rules.netlify.app/

Installation

Install the plugin from npm:

npm install eleventy-plugin-speculation-rules --save-dev

Add it to your Eleventy Config file:

const eleventyPluginSpeculationRules = require('eleventy-plugin-speculation-rules');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyPluginSpeculationRules);
};

Advanced usage:

You can override the default configuration by passing an object to the plugin:

const eleventyPluginSpeculationRules = require('eleventy-plugin-speculation-rules');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyPluginSpeculationRules, {
        mode: 'prerender', // prefetch | prerender
        eagerness: 'moderate', // conservative | moderate | eager
        noPrerenderSelector: '.no-prerender',
        include: [],
        exclude: []
    });
};

A filter can be used to exclude certain URL paths from being eligible for prefetching and prerendering. Alternatively, you can add the ‘no-prerender’ CSS class to any link ( tag) that should not be prerendered.

Speculation Mode

  • 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.

Speculation Eagerness

  • conservative - The browser will only speculate on the URL when it is highly confident that the user will navigate to it.
  • moderate - The browser will speculate on the URL when it is somewhat confident that the user will navigate to it.
  • eager - The browser will speculate on the URL when it is somewhat confident that the user will navigate to it.

The 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.

Browser support

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).

Other browsers will not see any adverse effects, however the feature will not work for those clients.

Contributing

If you notice an issue, feel free to open an issue.

  1. Fork this repo
  2. Clone git clone git@github.com:reatlat/eleventy-plugin-speculation-rules.git
  3. Install dependencies npm install
  4. Build npm run build
  5. Serve locally npm run dev

License

The code is available under the MIT license.

About

An Eleventy 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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published