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 Speculation Rules settings and UI #939

Merged
merged 16 commits into from
Jan 17, 2024

Conversation

felixarntz
Copy link
Member

@felixarntz felixarntz commented Jan 16, 2024

Summary

Fixes #906

Relevant technical choices

  • Setting uses an associative array, to avoid using multiple options in the database.
  • UI to control the settings is added to Settings > Reading.
  • See the spec for additional context on the available choices.
  • Test coverage is included.
  • Includes Fix new WPCS violation after trunk refresh #937, just to prevent irrelevant lint failure.

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@felixarntz felixarntz added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] JS & CSS no milestone PRs that do not have a defined milestone for release [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) labels Jan 16, 2024
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
'enum' => array_keys( plsr_get_mode_labels() ),
),
'eagerness' => array(
'description' => __( 'Whether to trigger on click (conservative), on hover (moderate), or on even the slight suggestion (eager) that the user may navigate to the URL.', 'performance-lab' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

if any change is made above it should be reflected here

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a good way for me to stay in the loop on this? If not, it would be great if you could ping me should there be related discussions.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's an internal mailing list where Chrome stuff spanning this area is discussed, and sometimes that includes status updates (though it also has other discussion that may be noise to you). I'm in the process of refreshing an evergreen status doc, too. I'll try to remember to send it to you when done.

modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/helper.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
modules/js-and-css/speculation-rules/settings.php Outdated Show resolved Hide resolved
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Almost there!

Comment on lines 53 to 59
$option = get_option( 'plsr_speculation_rules' );
if ( ! $option ) {
$option = plsr_get_setting_default();
}

$mode = $option['mode'];
$eagerness = $option['eagerness'];
Copy link
Member

Choose a reason for hiding this comment

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

While unlikely, couldn't it be that perhaps $option['mode'] isn't set here? If someone put an array into the plsr_speculation_rules without setting that key, then either $option['mode'] or $option['eagerness'] may not be set. So for extra defensive coding, I think this would be even more robust:

Suggested change
$option = get_option( 'plsr_speculation_rules' );
if ( ! $option ) {
$option = plsr_get_setting_default();
}
$mode = $option['mode'];
$eagerness = $option['eagerness'];
$option = get_option( 'plsr_speculation_rules' );
if ( ! is_array( $option ) ) {
$option = array();
}
$option = array_merge( plsr_get_setting_default(), $option );
$mode = $option['mode'];
$eagerness = $option['eagerness'];

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 is extremely unlikely as in the scenario you're describing the value would still always be sanitized at the lowest level (within update_option()) to include both values, per the setting registration.

Still, fair enough to do this from a defensive coding perspective. Updated in d490f93

foreach ( $fields as $slug => $args ) {
add_settings_field(
"plsr_speculation_rules_{$slug}",
$args['title'],
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if my suggestion here is warranted as I don't recall how this argument is even used versus what is passed to plsr_render_settings_field()

Suggested change
$args['title'],
esc_html( $args['title'] ),

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 that's needed, as the output of this is handled by core and thus responsibility of core. This isn't something WPCS complains about, so I don't think it's needed here.

@adamsilverstein
Copy link
Member

I tested this in Chrome Canary and verified the speculation rules are working.

Checking the page source, I see the json object:
image

Also, I noticed the plugin generator tag includes the js-and-css/speculation-rules string.

Checking DevTools -> Application->Speculative Loads->Speculations I can see that all of the links on my (local test) homepage are included. When I hover over a link, I see that the prerender is triggered and the "Ready":
image

Finally, even on my local machine clicking these prerendered links is notably faster!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Fantastic!

@adamsilverstein
Copy link
Member

Not sure what this message is about, I only added my review and I still see them as reviewers so 🤷🏼

image

@felixarntz felixarntz merged commit 13bc154 into feature/speculationrules Jan 17, 2024
7 checks passed
@felixarntz felixarntz deleted the add/speculation-rules-settings branch January 17, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants