Make WordPress Core

Changeset 59837


Ignore:
Timestamp:
02/18/2025 10:30:05 PM (5 weeks ago)
Author:
flixos90
Message:

General: Add speculative loading support via the Speculation Rules API.

This changeset adds support for the Speculation Rules API and configures it by default to prefetch certain links with an eagerness of conservative, leading to improved performance by starting to load URLs before the user lands on them.

The new WP_Speculation_Rules class is a container class representing the set of used speculation rules. By default, WordPress Core will only add a single speculation rule, which results in most links being prefetched conservatively.

The behavior of that main speculation rule can be altered by using the new wp_speculation_rules_configuration filter, which receives an associative array with mode and eagerness keys, or null. Both mode and eagerness have a default value of auto, which for now will result in the aforementioned behavior. The value null is used by default in certain scenarios such as when the current user is logged in. Developers can explicitly provide supported mode values (prefetch or prerender) and other supported eagerness values (conservative, moderate, or eager) to override and enforce the respective behaviors, or return null to disable speculative loading feature (either unconditionally or for certain situations). The Speculative Loading feature plugin for example, which this feature is based on, will make use of this filter to continue to use mode prerender and eagerness moderate by default. Developers can call the wp_get_speculation_rules_configuration() function to check how speculative loading is configured on the WordPress site.

Another important filter introduced is wp_speculation_rules_href_exclude_paths, which allows to expand the list of URL patterns that are excluded from being prefetched or prerendered per WordPress Core's main speculation rule configuration. Several URL patterns such /wp-admin/* (any URL within WP Admin) or /*\\?(.+) (any URL that includes query parameters) are already excluded by default. Plugins that use content that would be preferable not to prefetch or prerender can use the filter to provide corresponding URL patterns.

More advanced customization is possible by adding further speculation rules that will be loaded in addition to WordPress Core's main speculation rule. This can be achieved via the new wp_load_speculation_rules action, which receives the WP_Speculation_Rules class instance and can amend it as needed.

Props flixos90, westonruter, joemcgill, desrosj, mukesh27, tunetheweb, thelovekesh, adamsilverstein, swissspidy, domenicdenicola, jeremyroman.
Fixes #62503.

Location:
trunk
Files:
9 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r59712 r59837  
    359359add_action( 'wp_head', 'wp_custom_css_cb', 101 );
    360360add_action( 'wp_head', 'wp_site_icon', 99 );
     361add_action( 'wp_footer', 'wp_print_speculation_rules' );
    361362add_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
    362363add_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
  • trunk/src/wp-settings.php

    r59803 r59837  
    406406require ABSPATH . WPINC . '/interactivity-api/interactivity-api.php';
    407407require ABSPATH . WPINC . '/class-wp-plugin-dependencies.php';
     408require ABSPATH . WPINC . '/class-wp-url-pattern-prefixer.php';
     409require ABSPATH . WPINC . '/class-wp-speculation-rules.php';
     410require ABSPATH . WPINC . '/speculative-loading.php';
    408411
    409412add_action( 'after_setup_theme', array( wp_script_modules(), 'add_hooks' ) );
Note: See TracChangeset for help on using the changeset viewer.