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.