Skip to content

Add Speculative Loading opt-in for authenticated requests #2097

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

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

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jul 24, 2025

Speculative Loading was omitted from logged-in users per #1157:

Prefetching really shines with page caching as the server is able to deliver the content almost instantly with near zero CPU usage without waiting for the page to be generated. Logged in visits are predominately dynamic non cacheable requests.

See also #1157 (comment):

The ability to have speculation rules omitted by default for logged-in users came up today in the support forum.

One concern I have about doing this is that a user may activate Speculative Loading and then notice that the plugin doesn't seem to do anything. In this case, maybe it should be enabled for logged-out users and for admin users (who can activate plugins)? Presumably there would not be many admin users navigating around the site while being logged-in, so they wouldn't contribute significantly to the server load. What this means is that logged-in users who are not administrators would not get speculative loading by default, such as in the case of an e-commerce site or membership site.

There could be a filter to customize whether speculation rules are added to the page. For example, the plugin could do:

$should_print = apply_filters( 
    'plsr_print_speculation_rules', 
    ! is_user_logged_in() || current_user_can( 'activate_plugins' ) 
);

In this way, if a site wants to force speculation rules to always be printed, they could add this plugin code:

add_filter( 'plsr_print_speculation_rules', '__return_true' );

Or they could add their own arbitrary conditions for whether to print the speculation rules.

The important thing is to have the right defaults for the 80% of sites.

Nevertheless, if the server is able to handle the load of additional dynamic non-cacheable requests, there should be an opt-in to allow users to enable Speculative Loading for logged-in users. For sites where users are primarily logged-in (for example e-commerce, forums, social, membership) there should already be infrastructure in place to scale for non-cached responses. Users of such sites should be able to benefit from Speculative Loading as well. In fact, Speculative Loading is even more necessary for such sites since uncached requests involve more latency.

Being able to opt in to Speculative Loading would allow the administrators to experience the site more closely to what regular visitors experience, without first having to log out. In order to facilitate this specifically, the opt-in can limit to just administrators in addition to unauthenticated visitors. (Maybe the plugin should even default to enabling Speculative Loading for administrators, so they are able to see the impact of the plugin after it is activated.)

This is related to the No-cache BFCache plugin which allows for instant back/forward navigations to be enabled for authenticated pages. With this PR, authenticated pages also can leverage Speculative Loading. This greatly improves the experience of a logged-in user browsing around a site, perhaps the single user who browses the site the most: the administrator!

This adds a new option to the settings screen:

image

Adding an such an option to the plugin does not violate the 80% principle since it's not targeting core.

To Do

  • Update tests to account for new setting.

@westonruter westonruter added this to the speculation-rules n.e.x.t milestone Jul 24, 2025
@westonruter westonruter requested a review from felixarntz as a code owner July 24, 2025 15:18
@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) labels Jul 24, 2025
Copy link

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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>

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

@westonruter westonruter marked this pull request as draft July 24, 2025 15:42
Copy link

codecov bot commented Jul 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.32%. Comparing base (e147007) to head (9649da1).

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2097      +/-   ##
==========================================
+ Coverage   67.18%   67.32%   +0.13%     
==========================================
  Files          93       93              
  Lines        7750     7783      +33     
==========================================
+ Hits         5207     5240      +33     
  Misses       2543     2543              
Flag Coverage Δ
multisite 67.32% <100.00%> (+0.13%) ⬆️
single 36.59% <48.07%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@westonruter westonruter marked this pull request as ready for review July 24, 2025 20:30
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2025 Jul 24, 2025
@westonruter westonruter moved this from Not Started/Backlog 📆 to Code Review 👀 in WP Performance 2025 Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Code Review 👀
Development

Successfully merging this pull request may close these issues.

1 participant