Skip to content

Register runtime as dependency if found#48

Merged
kadamwhite merged 7 commits into
mainfrom
load-single-runtime
Feb 17, 2026
Merged

Register runtime as dependency if found#48
kadamwhite merged 7 commits into
mainfrom
load-single-runtime

Conversation

@mattheu
Copy link
Copy Markdown
Member

@mattheu mattheu commented Aug 11, 2022

Something we have faced when updating @humanmade/webpack-helpers to Webpack v5 is that hot reloading does not work correctly when you have multiple JS files emitted by a single webpack configuration and loaded on a page at the same time.

The issue is caused by the fact that each file emitted has the webpack runtime included. The fix for this is to split that into a separate file by setting optimization: { runtimeChunk: 'single' } in your webpack dev config. This spits out a new JS file runtime.js that needs to be loaded once per page.

What does this PR do? It detects if runtime.js is present in the manifest, and if so it registers that script and sets it as a dependency of the asset being loaded. This ensures it is always loaded, but only once, on the page

Copy link
Copy Markdown
Collaborator

@kadamwhite kadamwhite left a comment

Choose a reason for hiding this comment

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

One process question and two style nits

Comment thread inc/namespace.php Outdated
Comment thread inc/namespace.php Outdated
Comment thread inc/namespace.php Outdated
$handles = [];

if ( is_css( $asset_uri ) ) {
// Don't set runtime JS as dependency of a CSS file.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we call is_css( $asset_uri ) again up above, and use that as the condition for looking up the runtime, instead of having to undo it later? Something like,

if ( $runtime && ! is_css( $asset_uri ) ) {

mattheu and others added 3 commits August 15, 2022 16:52
Co-authored-by: K Adam White <kadamwhite@users.noreply.github.com>
Co-authored-by: K Adam White <kadamwhite@users.noreply.github.com>
@mattheu
Copy link
Copy Markdown
Member Author

mattheu commented Aug 15, 2022

Feedback addressed.

Copy link
Copy Markdown
Collaborator

@kadamwhite kadamwhite left a comment

Choose a reason for hiding this comment

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

Thanks @mattheu !

Can we add this to the changelog? We'll need to document this magic somewhere too but the docs generally are a bit bare rn

Comment thread inc/namespace.php Outdated
$asset_version = Manifest\get_version( $asset_uri, $manifest_path );
$is_asset_css = is_css( $asset_uri ); // Note returns false for the CSS dev JS fallback.


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's a small thing, but the double-spacing here is breaking linting.

Comment thread inc/namespace.php Outdated
@kadamwhite kadamwhite merged commit 3b7e38f into main Feb 17, 2026
0 of 2 checks passed
@kadamwhite kadamwhite deleted the load-single-runtime branch February 17, 2026 22:55
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.

2 participants