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

Prevent speculatively loading links to the uploads, content, plugins, template, or stylesheet directories #1167

Merged

Conversation

westonruter
Copy link
Member

This was brought up by @AntonyXSI in #1157:

3. Excluding links that contain "." or file extensions i.e to prevent a user from downloading large PDFs or images potentially bottlenecking bandwidth. An example would be images being wrapped in a link to the original uncropped image. Hovering over images in a gallery for example could result in lots of large unoptimized images being downloaded unnecessarily in the background. As images will begin to render almost instantly prerendering images, pdfs etc may not be as beneficial as prerendering pages. I'm also not sure what happens if .exe,.pdf or other large files are prefetched that would normally be downloaded once clicked? Does Chrome drop the request or continue the download from the same prefetch request?

See Chrome's behavior for speculative loading of an image and a PDF: #1157 (comment).

In this PR, the uploads directory is excluded from speculative loading. The uploads directory path is added to the base href excludes.

Before After
image image

What if the uploads directory is pointing to a CDN instead? Well, it will be ignored anyway because the speculation rules are configured to only do same-origin speculative loads:

$rules = array(
array(
'source' => 'document',
'where' => array(
'and' => array(
// Include any URLs within the same site.
array(
'href_matches' => $prefixer->prefix_path_pattern( '/*' ),
),

So they will be excluded anyway. (The same goes for a site with a site customizing the site_url to point to another domain, cf. #1164). But what if someone wants to do cross-origin speculative loads? That could be made possible with #1156 where the href_matches is changed from /* to instead be *://*/*. Nevertheless, it seems Chrome does not currently support this:

image

Nevertheless, the above was specifically for prerendering. If I try instead with the mode set to prefetch, I get different results:

image

So it can work with prefetch in some situations.

We should keep that in mind for the future.

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Apr 19, 2024
@westonruter westonruter added this to the speculation-rules n.e.x.t milestone Apr 19, 2024
Copy link

github-actions bot commented Apr 19, 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.

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 <westonruter@git.wordpress.org>
Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>

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

@felixarntz
Copy link
Member

@westonruter Would it make sense to instead prevent speculative loading of any links to something within wp-content? Uploads is probably most important, but I'm not sure we'd want to speculatively load anything bundled in a plugin or theme either, as it wouldn't be a regular WordPress URL.

@westonruter
Copy link
Member Author

@felixarntz yeah, good point. That should be added as another entry specific to wp-content since the uploads directory can be configured to point somewhere else.

@westonruter
Copy link
Member Author

For completeness, the following URLs should be excluded: WP_CONTENT_URL, WP_PLUGIN_URL, get_stylesheet_directory_uri(), and get_template_directory_uri(). All of these can be overridden from their defaults.

@westonruter westonruter changed the title Prevent speculatively loading links to the uploads directory Prevent speculatively loading links to the uploads, content, plugins, template, or stylesheet directories Apr 25, 2024
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.

Nice work!

Copy link
Contributor

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

LGTM

@westonruter westonruter merged commit 0410600 into trunk Apr 29, 2024
25 checks passed
@westonruter westonruter deleted the update/uploads-dir-excluded-from-speculative-loading branch April 29, 2024 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants