Skip to content
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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 21, 2024

This PR adds speculative loading support to WordPress Core, as outlined in the Trac ticket:

  • Introduce function wp_get_speculation_rules_configuration() to get the current speculation rules configuration (consisting of "mode" and "eagerness").
    • The default values for mode and eagerness are 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.
    • A new filter 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.
  • Introduce function wp_get_speculation_rules() to compute the full data for speculation Rules JSON output, based on the given configuration (return value from wp_get_speculation_rules_configuration()). This function is mostly a copy of the plugin's plsr_get_speculation_rules() function.
    • By default, the configuration will lead to speculative loading of all frontend URLs, while excluding patterns like /wp-admin/*, /wp-login.php, etc which should never be speculatively loaded.
    • A new filter 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 or prerender) as context, which thus allows to add exclusions depending on which mode is currently used.
    • While the filter can be used to add exclusions, it cannot remove any of the default exclusions. This is because they are simply not safe to prefetch / prerender because of their dynamic nature.
    • The default configuration also excludes prefetching links that use a no-prefetch class on their anchor, as well as excludes prerendering links that use a no-prerender class.
  • Introduce function wp_print_speculation_rules() which is hooked into wp_footer to print the default speculation rules JSON.
  • Introduce internal class WP_URL_Pattern_Prefixer, which is a direct port of the plugin's PLSR_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.

Copy link

github-actions bot commented Nov 21, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props flixos90, swissspidy, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

_doing_it_wrong(
__FUNCTION__,
sprintf(
esc_html__( 'The %s parameter was provided with an invalid value.' ),
Copy link
Member

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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function wp_get_speculation_rules_configuration() {
function wp_get_speculation_rules_configuration(): ?array {

Copy link
Member Author

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?

Copy link
Member

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 ) {
Copy link
Member

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:

Suggested change
if ( false === $config ) {
if ( ! is_array( $config ) ) {

Copy link
Member Author

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).

Copy link
Member

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.

Copy link
Member

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:

Suggested change
if ( false === $config ) {
if ( null === $config ) {

Comment on lines +15 to +16
* @return array<string, string>|false Associative array with 'mode' and 'eagerness' keys, or false if speculative
* loading is disabled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

Comment on lines +40 to +44
* @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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

src/wp-includes/speculative-loading.php Show resolved Hide resolved
src/wp-includes/speculative-loading.php Outdated Show resolved Hide resolved
*/
function wp_print_speculation_rules(): void {
$configuration = wp_get_speculation_rules_configuration();
if ( false === $configuration ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( false === $configuration ) {
if ( ! is_array( $configuration ) ) {

Copy link
Member Author

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.

Copy link
Member

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:

Suggested change
if ( false === $configuration ) {
if ( null === $configuration ) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants