Enables browsers to speculatively prerender or prefetch pages when hovering over links.
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.
See the Speculation Rules WICG specification draft.
By default, the plugin is configured to prerender WordPress frontend URLs when the user hovers over a relevant link. This can be customized via the “Speculative Loading” section under Settings > Reading.
A filter can be used to exclude certain URL paths from being eligible for prefetching and prerendering (see FAQ section). Alternatively, you can add the ‘no-prerender’ CSS class to any link (<a>
tag) that should not be prerendered. See FAQ for more information.
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.
This plugin was formerly known as Speculation Rules.
speculation-rules
folder to the /wp-content/plugins/
directory.Not every URL can be reasonably prerendered. Prerendering static content is typically reliable, however prerendering interactive content, such as a logout URL, can lead to issues. For this reason, certain WordPress core URLs such as /wp-login.php
and /wp-admin/*
are excluded from prefetching and prerendering. Additionally, any URL generated with wp_nonce_url()
(or which contain the _wpnonce
query var) is also ignored. You can exclude additional URL patterns by using the plsr_speculation_rules_href_exclude_paths
filter.
This example would ensure that URLs like https://example.com/cart/
or https://example.com/cart/foo
would be excluded from prefetching and prerendering.
<?php add_filter( 'plsr_speculation_rules_href_exclude_paths', function ( array $exclude_paths ): array { $exclude_paths[] = '/cart/*'; return $exclude_paths; } );
Keep in mind that sometimes it may be useful to exclude a URL from prerendering while still allowing it to be prefetched. For example, a page with client-side JavaScript to update user state should probably not be prerendered, but it would be reasonable to prefetch.
For this purpose, the plsr_speculation_rules_href_exclude_paths
filter receives the current mode (either “prefetch” or “prerender”) to provide conditional exclusions.
The following example would ensure that URLs like https://example.com/products/...
cannot be prerendered, while still allowing them to be prefetched.
<?php add_filter( 'plsr_speculation_rules_href_exclude_paths', function ( array $exclude_paths, string $mode ): array { if ( 'prerender' === $mode ) { $exclude_paths[] = '/products/*'; } return $exclude_paths; }, 10, 2 );
As mentioned above, adding the no-prerender
CSS class to a link will prevent it from being prerendered (but not prefetched). Additionally, links with rel=nofollow
will neither be prefetched nor prerendered because some plugins add this to non-idempotent links (e.g. add to cart); such links ideally should rather be buttons which trigger a POST request or at least they should use wp_nonce_url()
.
Prerendering can affect analytics and personalization.
For client-side JavaScript, is recommended to delay these until the page clicks and some solutions (like Google Analytics) already do this automatically for prerender. See Impact on Analytics. Additionally, cross-origin iframes are not loaded until activation which can further avoid issues here.
Speculating on hover (moderate) increases the chance the page will be loaded, over preloading without this signal, and thus reduces the risk here. Alternatively, the plugin offers to only speculate on mouse/pointer down (conservative) which further reduces the risk here and is an option for sites which are concerned about this, at the cost of having less of a lead time and so less of a performance gain.
A prerendered page is linked to the page that prerenders it, so personalisation may already be known by this point and changes (e.g. browsing other products, or logging in/out) may require a new page load, and hence a new prerender anyway, which will take these into account. But it definitely is something to be aware of and test!
Feedback is encouraged and much appreciated, especially since this plugin may contain future WordPress core features. If you have suggestions or requests for new features, you can submit them as an issue in the WordPress Performance Team’s GitHub repository. If you need help with troubleshooting or have a question about the plugin, please create a new topic on our support forum.
The Performance team and WordPress community take security bugs seriously. We appreciate your efforts to responsibly disclose your findings, and will make every effort to acknowledge your contributions.
To report a security issue, please visit the WordPress HackerOne program.
Contributions are always welcome! Learn more about how to get involved in the Core Performance Team Handbook.
Bug Fixes
Enhancements
Bug Fixes
Documentation
Enhancements
Bug Fixes