-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add speculative loading support #7860
base: trunk
Are you sure you want to change the base?
Add speculative loading support #7860
Conversation
… of setting, with expanded test coverage.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
_doing_it_wrong( | ||
__FUNCTION__, | ||
sprintf( | ||
esc_html__( 'The %s parameter was provided with an invalid value.' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing translator comment
* @return array<string, string>|false Associative array with 'mode' and 'eagerness' keys, or false if speculative | ||
* loading is disabled. | ||
*/ | ||
function wp_get_speculation_rules_configuration() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function wp_get_speculation_rules_configuration() { | |
function wp_get_speculation_rules_configuration(): ?array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would only work if we changed the supported non-array value to null
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct.
$config = apply_filters( 'wp_speculation_rules_configuration', $config ); | ||
|
||
// Allow the value `false` to indicate that speculative loading is disabled. | ||
if ( false === $config ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if the switch from false
to null
isn't applied, this suggestion would still stand:
if ( false === $config ) { | |
if ( ! is_array( $config ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should do this, as returning a value that's neither false
(or null
if we change it) nor an array is invalid, so in that case it makes more sense to go with the default behavior rather than disabling it (because an invalid filter value should not change the behavior, as it could be unintended).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that makes sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So then in that case, if we go with null
:
if ( false === $config ) { | |
if ( null === $config ) { |
* @return array<string, string>|false Associative array with 'mode' and 'eagerness' keys, or false if speculative | ||
* loading is disabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @return array<string, string>|false Associative array with 'mode' and 'eagerness' keys, or false if speculative | |
* loading is disabled. | |
* @return array<string, string>|null Associative array with 'mode' and 'eagerness' keys, or null if speculative | |
* loading is disabled. |
* @param array<string, string>|false $config Associative array with 'mode' and 'eagerness' keys. The default value | ||
* for both of them is 'auto'. Other possible values for 'mode' are | ||
* 'prefetch' and 'prerender'. Other possible values for 'eagerness' are | ||
* 'eager', 'moderate', and 'conservative'. Alternatively, you may | ||
* return `false` to disable speculative loading entirely. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param array<string, string>|false $config Associative array with 'mode' and 'eagerness' keys. The default value | |
* for both of them is 'auto'. Other possible values for 'mode' are | |
* 'prefetch' and 'prerender'. Other possible values for 'eagerness' are | |
* 'eager', 'moderate', and 'conservative'. Alternatively, you may | |
* return `false` to disable speculative loading entirely. | |
* @param array<string, string>|null $config Associative array with 'mode' and 'eagerness' keys. The default value | |
* for both of them is 'auto'. Other possible values for 'mode' are | |
* 'prefetch' and 'prerender'. Other possible values for 'eagerness' are | |
* 'eager', 'moderate', and 'conservative'. Alternatively, you may | |
* return `null` to disable speculative loading entirely. |
*/ | ||
function wp_print_speculation_rules(): void { | ||
$configuration = wp_get_speculation_rules_configuration(); | ||
if ( false === $configuration ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( false === $configuration ) { | |
if ( ! is_array( $configuration ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above, I think an explicit check for false
(or null
) makes more sense. It should not be possible to return any other values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, as wp_get_speculation_rules_configuration()
would be typed to return either an array
or false
/null
. In the latter case:
if ( false === $configuration ) { | |
if ( null === $configuration ) { |
Co-authored-by: Weston Ruter <[email protected]>
This PR adds speculative loading support to WordPress Core, as outlined in the Trac ticket:
wp_get_speculation_rules_configuration()
to get the current speculation rules configuration (consisting of "mode" and "eagerness").auto
, which later is evaluated into whatever the current Core default is. For the initial implementation at least, that default is to "prefetch" with "conservative" eagerness.wp_speculation_rules_configuration
can be used to override the behavior, either to change it to something more eager, or to hard-code it to the current default to force-control it, even if Core's default should change in the future.wp_get_speculation_rules()
to compute the full data for speculation Rules JSON output, based on the given configuration (return value fromwp_get_speculation_rules_configuration()
). This function is mostly a copy of the plugin'splsr_get_speculation_rules()
function./wp-admin/*
,/wp-login.php
, etc which should never be speculatively loaded.wp_speculation_rules_href_exclude_paths
can be used to add further URL patterns to exclude, relevant for plugins that may want to customize this. This filter receives the mode (prefetch
orprerender
) as context, which thus allows to add exclusions depending on which mode is currently used.no-prefetch
class on their anchor, as well as excludes prerendering links that use ano-prerender
class.wp_print_speculation_rules()
which is hooked intowp_footer
to print the default speculation rules JSON.WP_URL_Pattern_Prefixer
, which is a direct port of the plugin'sPLSR_URL_Pattern_Prefixer
class that handles safe prefixing of URL patterns with WordPress base paths (like home URL, site URL, plugins directory URL etc.).Trac ticket: https://core.trac.wordpress.org/ticket/62503
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.