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

Fix composition of href exclude paths to account for JSON encoding and differing site/home URLs #1164

Merged
merged 9 commits into from
Apr 19, 2024

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Apr 19, 2024

Summary

When filtering plsr_speculation_rules_href_exclude_paths to add additional paths, make sure the resulting de-duplicated array has sequential keys starting from zero. If this is not the case, then json_encode() interprets the array as an object which causes a speculation rules failure in the browser.

Additionally, we found that the base exclude hrefs were getting prefixed with the site URL path and then prefixed again with the home URL path with the merge with the additional filtered exclude href exclude paths. On sites with different site URL and home URL (e.g. /wp/ and /blog/) this can result in the base URLs getting double-prefixed in an erroneous prefix (e.g. /blog/wp/). This is fixed by avoiding the redundant prefixing, so that only the href exclude paths provided by the filter are prefixed with the home URL path. This PR also ensures that the nonce/_wpnonce action URLs (from #1143) are prefixed with the home URL path and not the site URL path.

Fixes #1163

Relevant technical choices

The result of calling array_unique() is passed into array_values() to ensure that the keys are numerically sequential from zero. Tests are updated to ensure this is the case.

Additionally, the $base_href_exclude_paths are no longer provided as the initial value to pass into the plsr_speculation_rules_href_exclude_paths filter. The values in $base_href_exclude_paths get merged again with the result of the filter, meaning they will always be present regardless. It is misleading to pass them into the filter, as a developer may think they can remove a path by eliminating it from the array passed to the filter when they cannot. Eliminating this redundancy alone fixes the symptom of non-sequential array keys, but the changes in this PR go further to ensure there is no possibility of non-sequential array keys when the the base exclude paths are merged with the return value from the filter.

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) labels Apr 19, 2024
@westonruter westonruter added this to the speculation-rules n.e.x.t milestone Apr 19, 2024
@westonruter westonruter requested a review from felixarntz as a code owner April 19, 2024 06:03
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 <[email protected]>
Co-authored-by: tunetheweb <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: joemcgill <[email protected]>
Co-authored-by: brianleejackson <[email protected]>

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

@westonruter
Copy link
Member Author

Build for testing: speculation-rules.zip

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 form a spec rules perspective but will leave others to comment on the PHP side of things.

@adamsilverstein adamsilverstein self-requested a review April 19, 2024 13:46
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.

Changes look good, thanks for the tests!

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

One question, but looks good.

array_map(
array( $prefixer, 'prefix_path_pattern' ),
array_merge(
$base_href_exclude_paths,
Copy link
Member

Choose a reason for hiding this comment

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

Now that $base_href_exclude_paths are being run through $prefixer::prefix_path_pattern here, should we avoid doing so above?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh! I think you identified another bug, specifically when the site_url and home_url are different. I've added a test case to show this: 74e52a2 & d77b46c. The problem is that the base exclude paths for wp-admin and wp-login are initially prefixed with the site URL, but then later after merging with the filter-supplied exclude paths, it is getting prefixed again and this time with the home URL. The result is the prefix from both the home URL and the site URL is getting applied to the base URLs. I've proposed a fix for this in 01d9153. Please check.

@westonruter westonruter requested a review from joemcgill April 19, 2024 16:20
@westonruter westonruter changed the title Ensure href exclude paths array is sequentially numeric so it is encoded as a JSON array and not as a JSON object Fix composition of href exclude paths to account for JSON encoding and differing site/home URLs Apr 19, 2024
@westonruter
Copy link
Member Author

Updated build for testing: speculation-rules.zip

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

This update looks good to me. Thanks for testing!

@westonruter westonruter merged commit 39f7a74 into trunk Apr 19, 2024
22 checks passed
@westonruter westonruter deleted the fix/filtering-exclude-paths branch April 19, 2024 18:16
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] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Speculative Loading invalid key for a URL pattern error - exclude_paths filter
4 participants