Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules: Load the import map polyfill when needed #57256

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Changes from all 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
26 changes: 16 additions & 10 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,21 @@ public static function print_module_preloads() {
* import maps (https://github.com/guybedford/es-module-shims/issues/406).
*/
public static function print_import_map_polyfill() {
$import_map = self::get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
wp_print_script_tag(
array(
'src' => gutenberg_url( '/build/modules/importmap-polyfill.min.js' ),
'defer' => true,
)
);
}
$test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")';
$src = gutenberg_url( '/build/modules/importmap-polyfill.min.js' );

echo (
// Test presence of feature...
'<script>( ' . $test . ' ) || ' .
/*
* ...appending polyfill on any failures. Cautious viewers may balk
* at the `document.write`. Its caveat of synchronous mid-stream
* blocking write is exactly the behavior we need though.
*/
'document.write( \'<script src="' .
$src .
'"></scr\' + \'ipt>\' );</script>'
);
}

/**
Expand Down Expand Up @@ -273,4 +279,4 @@ function gutenberg_dequeue_module( $module_identifier ) {
add_action( $modules_position, array( 'Gutenberg_Modules', 'print_module_preloads' ) );

// Prints the script that loads the import map polyfill in the footer.
add_action( 'wp_footer', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 );
add_action( 'wp_head', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
Loading