Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,27 @@ function register_asset( string $manifest_path, string $target_asset, array $opt
$asset_handle = $options['handle'] ?? $target_asset;
$asset_version = Manifest\get_version( $asset_uri, $manifest_path );

// If running the development build with runtimeChunk: single, a runtime file will be present in the manifest.
// Register this and ensure it is loaded only once per page.
Comment thread
mattheu marked this conversation as resolved.
Outdated
$runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' );
if ( $runtime ) {
$runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); // Ensure unique handle based on src.
Comment thread
mattheu marked this conversation as resolved.
Outdated
wp_register_script( $runtime_handle, $runtime );
$options['dependencies'][] = $runtime_handle;
}

// Track registered handles so we can enqueue the correct assets later.
$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 ) ) {

if ( isset( $runtime_handle ) ) {
$key = array_search( $runtime_handle, $options['dependencies'] );
if ( $key !== false ) {
unset( $options['dependencies'][ $key ] );
}
}

// Register a normal CSS bundle.
wp_register_style(
$asset_handle,
Expand Down