Skip to content

Commit

Permalink
Load the import map polyfill only when there is an import map (#56699)
Browse files Browse the repository at this point in the history
* Load polyfill only when there is an import map

* Remove the polyfill to check the performance impact

* Revert "Remove the polyfill to check the performance impact"

This reverts commit af5eaad.
  • Loading branch information
luisherranz authored Dec 4, 2023
1 parent 857b8b8 commit 8c4f435
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
12 changes: 0 additions & 12 deletions lib/experimental/interactivity-api/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ function gutenberg_register_interactivity_module() {
array(),
defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
);

// TODO: Replace with a simpler version that only provides support for import maps.
// TODO: Load only if the browser doesn't support import maps (https://github.com/guybedford/es-module-shims/issues/371).
wp_enqueue_script(
'es-module-shims',
gutenberg_url( '/build/modules/importmap-polyfill.min.js' ),
array(),
null,
array(
'strategy' => 'defer',
)
);
}

add_action( 'wp_enqueue_scripts', 'gutenberg_register_interactivity_module' );
23 changes: 23 additions & 0 deletions lib/experimental/modules/class-gutenberg-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ public static function print_module_preloads() {
}
}

/**
* Prints the necessary script to load import map polyfill for browsers that
* do not support import maps.
*
* TODO: Replace the polyfill with a simpler version that only provides
* support for import maps and load it only when the browser doesn't support
* import maps (https://github.com/guybedford/es-module-shims/issues/371).
*/
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,
)
);
}
}

/**
* Gets the module's version. It either returns a timestamp (if SCRIPT_DEBUG
* is true), the explicit version of the module if it is set and not false, or
Expand Down Expand Up @@ -193,3 +213,6 @@ function gutenberg_enqueue_module( $module_identifier ) {

// Prints the preloaded modules in the head tag.
add_action( 'wp_head', 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 );

0 comments on commit 8c4f435

Please sign in to comment.