Skip to content

Commit

Permalink
Do not load polyfill if there are no modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Jan 26, 2024
1 parent 91ea581 commit 1e94019
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/compat/wordpress-6.5/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,24 @@ public function print_import_map() {
* @since 6.5.0
*/
public function print_import_map_polyfill() {
$test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")';
$src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.min.js' );
$import_map = $this->get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
$test = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")';
$src = defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/modules/importmap-polyfill.min.js' ) : includes_url( 'js/dist/vendor/wp-polyfill-importmap.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>'
);
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>'
);
}
}
/**
* Returns the import map array.
Expand Down

0 comments on commit 1e94019

Please sign in to comment.