From 0d236d491f7206e12b211c53d0674c9b8b13dad8 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Thu, 30 Nov 2023 20:04:00 +0100 Subject: [PATCH 1/3] Load polyfill only when there is an import map --- .../interactivity-api/modules.php | 12 ---------- .../modules/class-gutenberg-modules.php | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/experimental/interactivity-api/modules.php b/lib/experimental/interactivity-api/modules.php index 02785a152ca1fa..0695da26f4b1b3 100644 --- a/lib/experimental/interactivity-api/modules.php +++ b/lib/experimental/interactivity-api/modules.php @@ -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' ); diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index ca74d863043ee6..5f847fa8c897ad 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -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 @@ -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 ); From af5eaad64acac068e6436f2367310edba9523145 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Thu, 30 Nov 2023 20:54:23 +0100 Subject: [PATCH 2/3] Remove the polyfill to check the performance impact --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index 5f847fa8c897ad..e26025ad62561c 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -215,4 +215,4 @@ function gutenberg_enqueue_module( $module_identifier ) { 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 ); +// add_action( 'wp_footer', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 ); From 063c18de69cb08a75eeaac275cbf0d6bf06ec122 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Mon, 4 Dec 2023 11:16:22 +0100 Subject: [PATCH 3/3] Revert "Remove the polyfill to check the performance impact" This reverts commit af5eaad64acac068e6436f2367310edba9523145. --- lib/experimental/modules/class-gutenberg-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/modules/class-gutenberg-modules.php b/lib/experimental/modules/class-gutenberg-modules.php index e26025ad62561c..5f847fa8c897ad 100644 --- a/lib/experimental/modules/class-gutenberg-modules.php +++ b/lib/experimental/modules/class-gutenberg-modules.php @@ -215,4 +215,4 @@ function gutenberg_enqueue_module( $module_identifier ) { 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 ); +add_action( 'wp_footer', array( 'Gutenberg_Modules', 'print_import_map_polyfill' ), 11 );