Loading...
banner plugin Image
woo-image

Speculative Loading

By WordPress Performance Team

Description

This plugin adds support for the Speculation Rules API, which allows defining rules by which certain URLs are dynamically prefetched or prerendered. This core Speculative Loading functionality was merged into WordPress 6.8, but it only prefetches with conservative eagerness by default. In contrast, this plugin defaults to prerendering with moderate eagerness when interacting with a link, and it provides a user interface to customize the mode and eagerness via the Speculative Loading section on the Settings > Reading admin screen.

By default, speculative loading is only enabled for logged-out users, since unauthenticated pages are typically only eligible for caching and so more efficient to prefetch/prerender. This means that sites with frequent logged-in users on the frontend—such as e-commerce, forums, or membership sites—will not benefit from the feature. If your server can handle the additional load, you can opt in to enable speculative loading for all logged-in users or for administrators only. This setting exclusively affects frontend pages; admin screens are always excluded.

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 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; they will simply not benefit from the speculative loading.

Other Notable Features

Here are a few other notable features of this free Speculative Loading plugin.

check-fill
Javascript
check-fill
Performance
check-fill
Prefetch
check-fill
Prerender
check-fill
Speculation Rules

FAQ

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 URLs generated with wp_nonce_url() (or which contains the _wpnonce query var) and nofollow links are also ignored. You can exclude additional URL patterns by using the plsr_speculation_rules_href_exclude_paths filter.

The following example ensures that URLs like https://example.com/cart/ or https://example.com/cart/foo are 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 ensures 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().

Contributors and developers

“Speculative Loading” is open source software. The following people have contributed to this plugin.

WPS
Exclamation-Image

6.47

Average

Star-Image

Ratings

5 out of 5 | 18
History-Image

Version

1.6.0
calendar-Image

Last updated

4 months ago
Star-Image

Active installations

70,000+
Star-Image

WordPress version

6.9.4 or higher
php-Image

PHP version

7.2 or higher
translate-Image

Languages

15

Tags

Javascript,Performance,Prefetch,Prerender,Speculation Rules