Skip to content

Boost: add URL-pattern exclusions to Defer Non-Essential JavaScript#49556

Draft
kraftbj wants to merge 2 commits into
trunkfrom
claude/boost-audit-prioritize-09rf6a-defer-js-excludes
Draft

Boost: add URL-pattern exclusions to Defer Non-Essential JavaScript#49556
kraftbj wants to merge 2 commits into
trunkfrom
claude/boost-audit-prioritize-09rf6a-defer-js-excludes

Conversation

@kraftbj

@kraftbj kraftbj commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #36417

Proposed changes

  • Adds an exclusion list to the Defer Non-Essential JavaScript module so specific pages can be excluded by URL pattern without disabling the feature site-wide.
  • New render_blocking_js_excludes data-sync entry (array of strings, default empty), persisted via the same reusable Minify_Excludes_State_Entry used by minify_js_excludes (trims/dedupes on save). At template_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 the do_shortcode_tag ignore-attribute filter is removed, so an excluded page is byte-identical to defer-disabled output. All other pages stay optimized.
  • Pattern semantics mirror the Page Cache bypass patterns users already know: path-only comparison (query strings ignored), (.*)/* 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-WordPress Request class and its data-sync entry), hence a self-contained matcher with the same documented behavior.
  • Implements Changes_Output_After_Activation so cached pages are invalidated when the list changes.
  • New collapsible "Exclude URL patterns" panel under the Defer JS module card, mirroring the existing minify-excludes MetaComponent (useDataSync + zod, notices, Tracks events). Its help text also documents the per-script data-jetpack-boost="ignore" attribute — closing the documentation gap from Boost: Include a tooltip on how to ignore JS files from defer #35461.
  • Adds 22 pattern-matching unit cases plus behavioral tests for the bail-out and normal paths.

Note for reviewers: this touches the same module as #49545 (Defer JS document.write fix); whichever merges second will need a trivial rebase.

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_toggle and defer_js_exceptions_save_clicked. No new user/site data is collected.

Testing instructions

  1. Enable Defer Non-Essential JavaScript. Load a front-end page logged out and view source: non-essential <script> tags are moved to just before </body>.
  2. In Boost settings, expand Exclude URL patterns under the Defer JS card, enter the page's path (e.g. checkout or gallery/(.*)), Save — a "Changes saved" notice appears.
  3. Reload the excluded page (logged out, cache cleared): markup should be identical to defer-disabled output (scripts in original positions, no data-jetpack-boost attributes). Comparing with curl against defer-off output should show byte-identical HTML.
  4. Load a different page: scripts are still deferred there.
  5. Reload the Boost admin page: saved patterns persist in the input and the "Except: …" summary.
  6. Optional: add data-jetpack-boost="ignore" to one script on a non-excluded page and confirm only that script stays in place.
  7. Unit tests: 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

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
@kraftbj kraftbj added [Status] In Progress [Plugin] Boost A feature to speed up the site and improve performance. [Boost Feature] Defer JS issues related to the Defer JS feature in Jetpack Boost [Tests] Includes Tests labels Jun 11, 2026 — with Claude
@jp-launch-control

jp-launch-control Bot commented Jun 11, 2026

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/plugins/boost/app/modules/optimizations/render-blocking-js/class-render-blocking-js.php 81/139 (58.27%) 48.05% -21 💚
projects/plugins/boost/app/lib/class-output-filter.php 11/13 (84.62%) 84.62% -22 💚

1 file is newly checked for coverage.

File Coverage
projects/plugins/boost/app/assets/src/js/features/render-blocking-js/render-blocking-js-meta.tsx 0/30 (0.00%) 💔

Full summary · PHP report · JS report

Coverage check overridden by Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR .

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
@kraftbj kraftbj added the Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR label Jun 11, 2026 — with Claude
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Boost Feature] Defer JS issues related to the Defer JS feature in Jetpack Boost Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR [Plugin] Boost A feature to speed up the site and improve performance. [Status] In Progress [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Boost: Add excludes to defer JS

3 participants