Boost: add URL-pattern exclusions to Defer Non-Essential JavaScript#49556
Boost: add URL-pattern exclusions to Defer Non-Essential JavaScript#49556kraftbj wants to merge 2 commits into
Conversation
Allow excluding specific pages from the Defer JS (render_blocking_js) module by URL pattern, so a page that breaks with defer enabled no longer forces disabling the feature site-wide (#36417). - New render_blocking_js_excludes data-sync entry (array of strings, defaults to empty), stored via the reusable excludes state entry. - Render_Blocking_JS bails out of output filtering at template_redirect when the request path matches an exclusion pattern, also removing the shortcode ignore-attribute filter so excluded pages are byte-identical to defer-disabled output. - Matching mirrors Page Cache bypass pattern semantics: path-only comparison, (.*) / * wildcards, optional trailing slash, case-insensitive; everything else is literal. - Admin UI: collapsible "Exclude URL patterns" input under the Defer JS module card, with help text that also documents the per-script data-jetpack-boost="ignore" escape hatch (#35461). - Cached pages are invalidated when the exclusion list changes. - Unit tests for the matching semantics and the bail-out behavior. https://claude.ai/code/session_01PgpTrtTCH4hpz6Krh3ssho
Code Coverage SummaryCoverage changed in 2 files.
1 file is newly checked for coverage.
Full summary · PHP report · JS report Coverage check overridden by
Coverage tests to be added later
|
Same fix as on the document.write branch: the unit suite runs without WordPress, so trunk code calling str_contains() needs the guarded bootstrap polyfill on PHP <= 7.4. https://claude.ai/code/session_01PgpTrtTCH4hpz6Krh3ssho
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Boost plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
There was a problem hiding this comment.
Pull request overview
Adds URL-pattern exclusions to Boost’s Defer Non-Essential JavaScript optimization so admins can exclude specific pages without disabling Defer JS site-wide, including UI to manage patterns and PHPUnit coverage for matching + bailout behavior.
Changes:
- Introduces a new data-sync entry (
render_blocking_js_excludes) and URL-pattern matcher to skip all Defer JS output filtering on matching requests. - Adds an “Exclude URL patterns” collapsible panel to the Defer JS module card (with Tracks events and help text including the per-script ignore attribute).
- Expands PHPUnit coverage with URL-matching semantics cases and tests for the excluded vs non-excluded output-filtering paths; adds a test bootstrap polyfill for
str_contains()in non-WP unit runs.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| projects/plugins/boost/app/modules/optimizations/render-blocking-js/class-render-blocking-js.php | Registers a new data-sync option and bails out of output filtering when the current request matches an excluded URL pattern. |
| projects/plugins/boost/app/assets/src/js/pages/index/index.tsx | Mounts the new Defer JS exclusions meta panel under the Defer JS module card. |
| projects/plugins/boost/app/assets/src/js/features/render-blocking-js/render-blocking-js-meta.tsx | Adds the UI for editing/saving excluded URL patterns and documents data-jetpack-boost="ignore". |
| projects/plugins/boost/app/assets/src/js/features/render-blocking-js/render-blocking-js-meta.module.scss | Styles the new exclusions meta panel. |
| projects/plugins/boost/tests/php/modules/optimizations/render-blocking-js/Render_Blocking_JS_Test.php | Adds URL-matching semantics tests and verifies excluded requests don’t attach output filtering / ignore-attribute behavior. |
| projects/plugins/boost/tests/bootstrap.php | Adds a guarded str_contains() polyfill for unit tests running without WordPress on PHP < 8.0. |
| projects/plugins/boost/changelog/add-defer-js-url-excludes | Changelog entry documenting the new Defer JS URL exclusions feature. |
Fixes #36417
Proposed changes
render_blocking_js_excludesdata-sync entry (array of strings, default empty), persisted via the same reusableMinify_Excludes_State_Entryused byminify_js_excludes(trims/dedupes on save). Attemplate_redirect, the request path is checked against the patterns; on a match the module leaves the page completely untouched — output filtering is never attached and thedo_shortcode_tagignore-attribute filter is removed, so an excluded page is byte-identical to defer-disabled output. All other pages stay optimized.(.*)/*wildcards, optional leading/trailing slashes, case-insensitive, full URLs accepted by stripping the home URL; everything else is treated literally (preg_quoted, so user input can't break the regex). The Page Cache helper itself wasn't cleanly reusable (split across the pre-WordPressRequestclass and its data-sync entry), hence a self-contained matcher with the same documented behavior.Changes_Output_After_Activationso cached pages are invalidated when the list changes.data-jetpack-boost="ignore"attribute — closing the documentation gap from Boost: Include a tooltip on how to ignore JS files from defer #35461.Related product discussion/links
Does this pull request change what data or activity we track or use?
Adds two UI interaction Tracks events consistent with existing minify-excludes panel events:
defer_js_excludes_panel_toggleanddefer_js_exceptions_save_clicked. No new user/site data is collected.Testing instructions
<script>tags are moved to just before</body>.checkoutorgallery/(.*)), Save — a "Changes saved" notice appears.data-jetpack-boostattributes). Comparing withcurlagainst defer-off output should show byte-identical HTML.data-jetpack-boost="ignore"to one script on a non-excluded page and confirm only that script stays in place.cd projects/plugins/boost && vendor/bin/phpunit-select-config phpunit.#.xml.dist --bootstrap tests/bootstrap.php --testsuite unit --filter Render_Blocking_JS_Test(19 tests, 45 assertions; full unit suite 117 tests passing).https://claude.ai/code/session_01PgpTrtTCH4hpz6Krh3ssho
Generated by Claude Code