Instant Page Load Times with Speculative Loading in WordPress

Speculative Loading in WordPress

Introduction: Speculative Loading in WordPress

In the ever-evolving digital landscape, website performance is a critical factor that can make or break user engagement and satisfaction. Slow page load times are a major deterrent for visitors, leading to high bounce rates and poor SEO performance. To address this, the WordPress Performance Team has introduced a groundbreaking plugin called “Speculative Loading in WordPress.” This new technology leverages the Speculation Rules API to automatically prerender certain URLs, significantly enhancing page load times and user experience.

Understanding Speculative Loading in WordPress

What is Speculative Loading in WordPress?

Speculative Loading in WordPress, often referred to as prefetching or prerendering, is a performance optimization technique where resources are preloaded in anticipation of user actions. By speculatively loading these resources, WordPress websites can deliver content almost instantaneously when users navigate to new pages.

How Does Speculative Loading in WordPress Work?

Speculative Loading in WordPress works by predicting user behavior and preloading resources based on these predictions. This involves loading scripts, images, and other assets before they are explicitly requested by the user. The Speculation Rules API, a new web API, enables this functionality by allowing developers to define rules for prefetching or prerendering URLs dynamically.

A Brief History of Prefetch and Prerender in WordPress

Historically, WordPress has utilized the Resource Hints API to inject <link> tags into HTML documents for prefetching or prerendering resources. While useful, this method lacked flexibility and efficiency. It required defining URLs to prefetch or prerender as soon as the HTML was loaded, which often led to either under-utilization or excessive prefetching of resources.

Solutions like Quicklink improved this by dynamically inserting <link> tags based on user viewport, but they still fell short by excessively prefetching resources and requiring a JavaScript library. Moreover, the link[rel="prerender"] approach didn’t support true prerendering and has since been deprecated.

Introducing the Speculation Rules API

Speculation Rules API Syntax

The Speculation Rules API addresses the limitations of previous methods by allowing developers to define rules for dynamically prefetching and prerendering URLs in JSON syntax. These rules can be configured to trigger based on user interactions, such as hovering over a link. Here’s an example of the API’s syntax:

<script type="speculationrules">
{
    "prerender": [
        {
            "source": "document",
            "where": {
                "and": [
                    {
                        "href_matches": "/*"
                    },
                    {
                        "not": {
                            "href_matches": [
                                "/wp-login.php",
                                "/wp-admin/*"
                            ]
                        }
                    }
                ]
            },
            "eagerness": "moderate"
        }
    ]
}
</script>

This configuration prerenders URLs matching the specified patterns, excluding those related to WP Admin or login pages, with a moderate eagerness level that triggers when the user hovers over the link.

Browser Support

The Speculation Rules API is currently supported in Chrome and Edge starting from version 121. While users on other browsers won’t benefit from this feature, they won’t experience any adverse effects either, making it a safe progressive enhancement.

The Speculative Loading Plugin for WordPress

Plugin Features and Settings

The new Speculative Loading plugin for WordPress, developed by the WordPress Performance Team, simplifies the implementation of speculative loading. Once activated, the plugin automatically prerenders URLs on the page with a default eagerness setting of “moderate.” This means that as users hover over links, the linked pages are prerendered in the background, ready to load instantly when clicked.

Customization Options

The Speculative Loading plugin for WordPress offers several customization options to suit different website needs. These settings can be accessed via the new “Speculative Loading” section in the Settings > Reading screen. Users can adjust the eagerness levels to “conservative” or “eager,” depending on their preference for performance versus resource usage. Additionally, site owners can exclude specific URLs from being prerendered using the plsr_speculation_rules_href_exclude_paths filter.

Here’s an example to exclude URLs related to a shopping cart:

add_filter(
    'plsr_speculation_rules_href_exclude_paths',
    function ( $exclude_paths ) {
        $exclude_paths[] = '/cart/*';
        return $exclude_paths;
    }
);

Installation Steps for Speculative Loading in WordPress

To install and activate the Speculative Loading plugin in WordPress, follow these simple steps:

  1. Login to Your WordPress Admin Dashboard:
  • Open your web browser and log in to your WordPress admin panel using your admin credentials.
  1. Navigate to the Plugins Section:
  • From the left-hand menu, click on Plugins and then select Add New.
  1. Search for the Speculative Loading Plugin:
  • In the search bar located in the top right corner, type Speculative Loading.
  1. Install the Plugin:
  • Locate the Speculative Loading plugin from the search results and click on the Install Now button.
  1. Activate the Plugin:
  • Once the installation is complete, click on the Activate button to enable the plugin on your site.
  1. Configure Plugin Settings:
  • Navigate to Settings > Reading and find the new Speculative Loading section.
  • Adjust the settings according to your preference, such as setting the eagerness level (moderate, conservative, or eager) and excluding specific URLs from prerendering.
  1. Save Changes:
  • After configuring the settings, click on the Save Changes button to apply your configurations.

By following these steps, you can easily install and activate the Speculative Loading plugin on your WordPress site, enhancing your website’s performance and providing a faster, more responsive user experience.

Future Potential of Speculative Loading in WordPress

The future potential of Speculative Loading in WordPress is immense. As web technologies continue to advance, the ability to preemptively load resources based on user behavior will become even more sophisticated. This could lead to more granular control over which elements are loaded and when, further optimizing the performance and responsiveness of WordPress sites. Additionally, as browser support for the Speculation Rules API expands, more users will benefit from this technology, making it an essential tool for web developers aiming to provide the fastest possible user experience. The seamless integration of Speculative Loading into WordPress core could set a new standard for web performance, potentially influencing best practices across the entire web development industry.

Speculative Loading in WordPress represents a significant leap forward in enhancing website performance. By leveraging the Speculation Rules API, this plugin can drastically reduce page load times, improving user experience and boosting SEO performance. As the plugin and the underlying API continue to evolve, they hold the potential to become a core feature of WordPress, revolutionizing how web content is delivered.

Enhancing your WordPress site’s performance with Speculative Loading is just the beginning. If you need expert assistance with your WordPress website, whether it’s optimizing performance, custom development, or any other aspect, we’re here to help. Contact us today to discuss how we can take your website to the next level!