Performance

Speculative Loading in WordPress: Instant Page Navigation with the Speculation Rules API

Speculative Loading in WordPress: Instant Page Navigation with the Speculation Rules API

Most performance work focuses on making a single page load faster. Speculative loading takes a different approach: it starts loading the next page before the visitor clicks. When they do click, the page is already there, or nearly there. Navigation feels instant.

This used to require JavaScript libraries that watched mouse movements and prefetched links. Now browsers have a built-in way to do it, the Speculation Rules API, and since WordPress 6.8, WordPress core uses it by default. Many site owners don’t know it’s running, what it does or how to adjust it.

Prefetching and prerendering

The Speculation Rules API supports two levels of speculation.

Prefetch downloads the HTML of the next page in advance. When the visitor clicks, the browser already has the document, so it skips the network request for the HTML. Images, CSS and scripts still need to load if they aren’t cached.

Prerender goes much further. The browser loads the next page completely in a hidden background tab, including all resources and JavaScript, and renders it. When the visitor clicks, the browser simply swaps it in. The result is a truly instant page change.

Prerendering gives a bigger benefit but costs more: it uses bandwidth, memory and CPU for a page the visitor might never open, and it runs JavaScript, including analytics, before the page is actually viewed.

Eagerness: when speculation starts

Speculation rules have an eagerness setting that controls when the browser starts loading:

  • Immediate. As soon as the rules are seen. Aggressive, best for a small number of very likely next pages.
  • Eager. Currently treated similarly to immediate in most browsers, though this may change.
  • Moderate. When the visitor hovers over a link for a short moment, about 200 milliseconds, or starts pressing it.
  • Conservative. Only when the visitor starts clicking or tapping. On touch devices, this is when the finger touches the screen.

Conservative gives only a small head start, typically 50 to 100 milliseconds, but wastes almost nothing. Moderate gives more time on desktop, where people hover before clicking, at the cost of occasionally loading pages that aren’t visited.

What WordPress does by default

WordPress 6.8 adds speculation rules to the front end for logged-out visitors. The default configuration:

  • Uses prefetch, not prerender
  • Uses conservative eagerness
  • Applies to links pointing to the same site
  • Excludes URLs that shouldn’t be speculatively loaded, such as wp-admin, wp-login.php, links with query strings commonly used for actions and URLs with nofollow attributes
  • Is disabled for logged-in users and when pretty permalinks are off

That’s a cautious default. It almost never causes problems, and it gives a small but real improvement to navigation speed.

You can see the rules by viewing the page source and looking for a script tag with type=”speculationrules”.

Tuning the configuration

WordPress provides a filter to change the mode and eagerness:

Returning null disables speculative loading entirely.

The Speculative Loading plugin from the WordPress Performance Team, which predates the core feature, adds a settings screen for this under Settings, Reading, so you can change it without code.

When to use prerender

Prerender with moderate eagerness can make a content site feel remarkably fast. It suits:

  • Blogs and content sites where pages are mostly static
  • Sites where JavaScript on each page is light
  • Sites where most visitors browse several pages

Be more cautious with:

  • Pages with heavy JavaScript or third-party widgets, because they’ll run in the background
  • Sites with strict data or bandwidth concerns
  • Pages that trigger actions on load

When to stay with prefetch

Prefetch is the safer choice for most business sites, WooCommerce stores and sites with complex scripts. It still helps, and it doesn’t run any JavaScript from the speculated page.

Excluding URLs

Some URLs should never be speculatively loaded. Typical examples:

  • Logout links, which would log the visitor out
  • Add-to-cart links that work through GET requests
  • Links that change state, like “mark as read” or “unsubscribe”
  • Very heavy pages, like large PDFs or interactive tools

WordPress excludes some of these automatically. You can add your own with another filter:

For individual links, you can add the class no-prefetch or no-prerender to opt them out.

In general, any link that changes something when visited with a GET request is a design problem anyway. Actions should use POST requests. But real sites have such links, so check yours.

Analytics and prerendering

When a page is prerendered, its JavaScript runs before the visitor sees it. If your analytics script fires a pageview immediately, you’d count views for pages the visitor never opened.

Google Analytics 4 and most major analytics tools handle this correctly. They detect prerendering and wait until the page is activated before recording a view. Smaller or custom tracking scripts may not. If you enable prerender, check that pageview counts don’t jump unexpectedly.

The same applies to ad impressions, chat widgets that send “visitor arrived” events and anything else that reports on load. Pages can check document.prerendering and listen for the prerenderingchange event to delay actions until the page is actually shown.

Browser support

The Speculation Rules API is supported in Chromium-based browsers: Chrome, Edge, Opera and others. Other browsers ignore the rules completely, so there’s no harm to their visitors, just no benefit. Since Chromium browsers make up the majority of traffic on most sites, the benefit is still substantial.

Browsers also apply their own limits. They cap the number of speculations, skip them when the device is in battery or data saver mode and cancel prerenders under memory pressure. You don’t need to handle these cases yourself.

Measuring the effect

Speculative loading improves navigation between pages, so it shows up in metrics for subsequent page loads rather than landing pages. In real user monitoring, look at LCP for non-landing page views. Prerendered pages often record LCP values close to zero.

In Chrome DevTools, the Application panel has a Speculative loads section showing which rules were found, which URLs were speculated and whether each prefetch or prerender was used. That’s the easiest way to confirm the rules are working.

Interaction with caching

Speculative loading works best when pages are served from a page cache. A prefetch request for a cached page is cheap for the server. An uncached page means each speculation runs PHP and database queries, which adds load, especially with moderate eagerness on a busy site.

If your site doesn’t have page caching, keep the conservative default until it does.

A recommended approach

For most sites:

  1. Leave the WordPress default on: prefetch, conservative.
  2. Add exclusions for cart, checkout, account and any action links.
  3. If you run a content site with good caching and light scripts, test prerender with moderate eagerness.
  4. Check analytics, ads and chat tools for double counting after switching to prerender.
  5. Use DevTools to confirm speculation is happening and pages are activated.

It’s one of the few performance improvements that costs almost nothing to enable and makes a site feel faster in a way visitors notice immediately.

Speculative loading and caching plugins

Some caching and optimisation plugins have their own link preloading features, which use JavaScript to prefetch pages on hover. Running those alongside core speculation rules is usually unnecessary and can lead to duplicate requests. If your caching plugin offers “preload links on hover” or similar, compare it with the core feature and keep only one.

Also check that your caching plugin doesn’t strip or combine the speculation rules script tag. Most handle it correctly, because the script has a type the browser doesn’t execute as normal JavaScript, but aggressive HTML or script optimisation can occasionally interfere. A quick look at the page source after enabling any optimisation feature confirms the rules are still present.

A note on WooCommerce stores

On stores, the benefit of speculative loading is strongest on category and product pages, where visitors move quickly from one product to the next. Keep cart, checkout and account pages excluded, and stick with prefetch unless your product pages are fully cached and have little JavaScript. Variation selectors, reviews widgets and recommendation engines can make prerendering expensive for limited gain.

Frequently asked questions

Will speculative loading increase my server load?

Slightly, because some speculated pages are never visited. With conservative eagerness the increase is tiny. With moderate or eager settings it’s larger, so make sure pages are cached.

Does it work for logged-in users?

Not by default in WordPress. Logged-in pages are often personalised and uncached, so speculation could add load or cause unexpected effects.

Can speculative loading break anything?

The prefetch default is very safe. Prerender can cause problems with scripts that act on load, such as analytics without prerender support or links that perform actions. Test before enabling.

How do I turn it off?

Return null from the wp_speculation_rules_configuration filter, or use the setting provided by the Speculative Loading plugin.