Skip to content

Boost: keep position-dependent inline scripts (document.write) in place when Defer JS is enabled#49545

Draft
kraftbj wants to merge 1 commit into
trunkfrom
claude/boost-audit-prioritize-09rf6a-defer-js-inline
Draft

Boost: keep position-dependent inline scripts (document.write) in place when Defer JS is enabled#49545
kraftbj wants to merge 1 commit into
trunkfrom
claude/boost-audit-prioritize-09rf6a-defer-js-inline

Conversation

@kraftbj

@kraftbj kraftbj commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #30012

Proposed changes

  • With "Defer Non-Essential JavaScript" enabled, inline scripts using document.write() / document.writeln() (e.g. added via a Custom HTML block) were relocated to after the footer, so their output rendered at the bottom of the page instead of where the block was placed.
  • During output filtering, inline <script> tags (no src) whose body contains document.write are now tagged with the existing data-jetpack-boost="ignore" attribute, so they execute in their original position. The check is an intentionally conservative case-insensitive substring match — a false positive merely leaves a script in place, which is the safe outcome.
  • No behavior change for: external scripts (src present, even if the inert body mentions document.write), plain inline scripts (still deferred), or scripts already carrying the ignore attribute (markup untouched, no duplicate attribute).
  • Adds 6 unit tests exercising the module's real handle_output_stream() + append_script_tags() pipeline.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

  1. On a test site, activate Jetpack Boost and enable Defer Non-Essential JavaScript.
  2. Edit a post and add a Custom HTML block in the middle of the content:
    <div style="border:2px solid red">
    <script>document.write("<strong>WRITTEN BY SCRIPT</strong>");</script>
    </div>
  3. Without this fix: view the post logged out — "WRITTEN BY SCRIPT" appears at the very bottom of the page after the footer; the red box is empty.
  4. With this fix: the text renders inside the red box where the block was placed; view source confirms the script tag stayed in the content and carries data-jetpack-boost="ignore".
  5. Confirm regular scripts are still deferred: view source and check theme/plugin scripts are still relocated to just before </body>.
  6. Unit tests: composer install --working-dir=projects/plugins/boost && cd projects/plugins/boost && vendor/bin/phpunit-select-config phpunit.#.xml.dist --bootstrap tests/bootstrap.php --testsuite unit --filter Render_Blocking_JS_Test (22 tests, 28 assertions passing; full Boost unit suite: 120 tests, 340 assertions).

https://claude.ai/code/session_01PgpTrtTCH4hpz6Krh3ssho


Generated by Claude Code

…is enabled

The Render Blocking JS (Defer Non-Essential JavaScript) module moves all
script tags to the end of the document. Inline scripts that call
document.write()/document.writeln() insert markup at the script's
location, so moving them renders their output after the footer instead
of where the block was placed (e.g. a Custom HTML block in post
content). See issue #30012.

Inline scripts (no src) whose body contains document.write are now
tagged with the existing ignore attribute during output filtering, so
they stay in their original position. Scripts already carrying the
ignore attribute, external scripts (with src), and plain inline scripts
are unaffected.

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
@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.

@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/boost/app/modules/optimizations/render-blocking-js/class-render-blocking-js.php 50/102 (49.02%) 38.79% -27 💚

Full summary · PHP report · JS report

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 [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] HTML Block with JavaScript Loads After Footer when Defer JS Enabled

2 participants