Skip to content

fix(theme): load theme scripts deferred instead of as modules - #514

Merged
oxyc merged 2 commits into
masterfrom
fix/theme-scripts-defer
Dec 3, 2026
Merged

oxyc merged 2 commits into
masterfrom
fix/theme-scripts-defer

Conversation

@oxyc

@oxyc oxyc commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Firefox ignores WordPress's import map when a module script has started loading before it. A classic theme gets the import map in the footer, and SageServiceProvider marked every sage/… and gds-… script type="module", so the menu scripts printed in the head started module loading first. Core's fit-text module then failed in Firefox with The specifier “@wordpress/interactivity” was a bare specifier.

The theme's builds are plain bundled scripts (no ES module output in the webpack config), so they now load deferred through core's loading strategy: late on wp_enqueue_scripts, every registered sage/…/gds-… handle without a strategy gets strategy: defer. Core prints the attribute, keeps dependencies in order (it falls back to blocking when a blocking script depends on one) and handles inline scripts, so there's no tag rewriting. Handles in assets.async_scripts stay with AsyncLoaderServiceProvider. A real ES module belongs in wp_enqueue_script_module(), which core prints after the import map.

Tests: theme and block handles get defer and print without type="module", other plugins' scripts are left alone, async handles aren't deferred.

🤖 Generated with Claude Code

oxyc and others added 2 commits December 3, 2026 08:46
Firefox ignores WordPress's import map once a module script has started
loading before it. A classic theme gets the import map in the footer,
and the theme marked its sage/ and gds- scripts type="module", so the
ones printed in the head started module loading first and core modules
(fit-text) failed to import @wordpress/interactivity. The builds are
plain scripts, so they load deferred; async ones stay async.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Set the defer strategy on sage/ and gds- handles instead of rewriting
their tags, so core prints the attribute, keeps dependencies in order
and handles inline scripts. Handles in assets.async_scripts stay with
the async loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oxyc
oxyc merged commit 6ef96b0 into master Dec 3, 2026
1 check passed
@oxyc
oxyc deleted the fix/theme-scripts-defer branch December 3, 2026 12:09
oxyc added a commit that referenced this pull request Dec 3, 2026
sage/app.js is in assets.deferred_scripts, so AsyncLoaderServiceProvider
adds defer to its tag; the core defer strategy from #514 added a second
one. Chrome drops the nonce of a script with a duplicated attribute, so
a nonce-based CSP blocks app.js.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant