Morteza.online

Supercharge Web App Speed by 90% with Speculation Rules

by Morteza on 1/31/2025

Imagine a user clicking a link on your site and the new page loads instantly, no white screens, no waiting, just seamless navigation. Sounds like magic, right? That’s exactly what the Speculation Rules API enables.

Traditional preloading and prefetching techniques in web development lack intelligence. They either fetch too aggressively, wasting resources, or not enough, leading to slow transitions. The Speculation Rules API changes the game by allowing the browser to smartly predict user behavior and preload or pre-render pages before they’re even needed.

In this article, we’ll explore:

  • What the Speculation Rules API is and how it works
  • Why it’s a big deal for performance
  • Practical examples of pre-fetching and pre-rendering
  • Eager Modes in the Speculation Rules API
  • How CDNs like Akamai can enhance its effectiveness
  • The potential bottlenecks and challenges to consider
  • Conclusion

What the Speculation Rules API is and how it works

The Speculation Rules API is a browser feature designed to improve page load times by intelligently prefetching or pre-rendering links based on user behavior.

It allows developers to define rules that tell the browser which pages to fetch in advance. Unlike traditional methods such as < link rel="prefetch">, this API adapts dynamically based on how users interact with a page, leading to smarter, resource-efficient optimizations.

This API introduces two main techniques:

  1. Prefetching: Downloads and caches the next page’s resources, so it loads faster when requested.
  2. Pre-rendering: Fully renders the next page in the background, making it instantly interactive when the user navigates to it.

It also provides different sources to define where speculation should be applied.

Understanding source in Speculation Rules

The source property in a speculation rule determines where the browser should look for links to prefetch or pre-render.

Source Option Description Use Case
"document" The rule applies to the entire document, scanning for all matching links. Best for global link handling (e.g., prefetching all product pages).
"list" A manual list of specific URLs to prefetch/pre-render. When you know in advance which pages are important.

By choosing the appropriate source, we can fine-tune preloading strategies based on our application’s needs.

Why it’s a big deal for performance

The Problem with Traditional Approaches

Web developers have relied on various preloading techniques for years, but they come with limitations:

  • Aggressive prefetching wastes bandwidth and can slow down users on weak connections.
  • Manual preloading (e.g., Next.js next/link prefetch) only works when users hover over or interact with links.
  • Service Workers can prefetch resources, but they don’t have fine-grained control over when to fetch.

The Speculation Rules API Advantage

The Speculation Rules API is smarter because it allows developers to:

  • Define conditions for when pages should be prefetched (e.g., when links are visible, when a user is likely to click, etc.).
  • Optimize performance dynamically, reducing unnecessary fetches while improving perceived speed.
  • Enable full-page pre-rendering for critical pages, making them instantly interactive.

This makes it perfect for any web application to benefit of this approach.

Example 1: Pre-fetching

Let’s say we want to prefetch all links on a page when they become visible in the viewport. We can achieve this using the Speculation Rules API like this:

`<script type="speculationrules"> 
{"prefetch": [
    {
      "source": "document",
      "eagerness": "moderate",
      "where": { "href_matches": "/product/.*" }
    }
]} </script>`

Let's explain the rule:

  • "source": "document" → The rule applies to the entire document.
  • "eagerness": "moderate" → Pages will be prefetched only when they are likely to be needed (not too aggressively).
  • "where": { "href_matches": "/product/.*" } → Applies prefetching only to product pages.

This approach ensures that product pages load faster without wasting bandwidth on unnecessary requests.

Example 2: Pre-rendering

Let’s take it a step further. If we want to fully pre-render pages before the user even clicks, we can use the following rule:

`<script type="speculationrules"> 
{"prerender": [
    {
      "source": "document",
      "eagerness": "conservative",
      "where": { "href_matches": "/checkout" }
    }
]} </script>`

Let's explain the rule:

  • When a user visits any page, the browser pre-renders the checkout page in the background.
  • When they click on a checkout link, the page appears instantly—no waiting.
  • "eagerness": "conservative" ensures the browser only pre-renders when it strongly predicts the user will navigate there (e.g., hovering over a "Checkout" button).

This is particularly useful for critical user flows, like checkout pages or sign-up pages, where instant interactions can increase conversion rates.

Eager Modes in the Speculation Rules API

The eagerness setting controls how aggressively the browser prefetches or pre-renders pages. Here's a breakdown:

Mode Behavior Use Case
"eager" User agents should enact the candidate on even a slight suggestion that the user may navigate to this URL in the future. For instance, the user might have moved the cursor toward a link or hovered it, even momentarily, or paused scrolling when the link is one of the more prominent ones in the viewport.
"moderate" User agents should enact the candidate if user behavior suggests the user may navigate to this URL in the near future. For instance, the user might have scrolled a link into the viewport and moved the cursor over it for some time.
"conservative" User agents should enact the candidate only when the user is very likely to navigate to this URL at any moment. For instance, the user might have begun to interact with a link.

Choosing the right eagerness level is key to balancing performance and resource usage.

How CDNs like Akamai can enhance its effectiveness

CDNs like Akamai can supercharge the Speculation Rules API by handling prefetching and caching at the network edge, reducing server load.

Here’s how:

  1. Edge Caching: Prefetched content is stored closer to users, speeding up retrieval.
  2. Intelligent Routing: Akamai can predict user behavior and optimize delivery based on global traffic patterns.
  3. Load Balancing: Pre-rendered pages can be served without hitting the origin server.

The potential bottlenecks and challenges to consider

While powerful, the Speculation Rules API isn’t perfect. Here are some considerations:

  • Browser Support
    • As of now, it’s still not supported in all browsers (primarily Chrome-based browsers).
    • Developers should feature-detect and fallback to traditional preloading where needed.
  • Resource Overuse
    • Poorly configured speculation rules can waste bandwidth and slow down performance instead of improving it.
    • Avoid overusing "eager" modes unless necessary.
  • SEO Considerations
    • Search engines may index pre-rendered pages differently.

Conclusion

The Speculation Rules API is a game-changer for all and any type web applications, offering smart and efficient prefetching and pre-rendering that can dramatically boost performance.

As browser support grows, this API could redefine how we approach performance optimizations in modern web applications. Now’s the perfect time to start experimenting with it in your web application.