Last active
September 5, 2025 21:58
-
-
Save westonruter/b4a1040de3b78fb28d56b9c8cab7da3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import foo from 'foo'; | |
console.log( 'Executed script module bar.' ); | |
export default {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bar from 'bar'; | |
console.log( 'Executed script module baz.' ); | |
export default {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log( 'Executed script module foo.' ); | |
export default {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Try Script Module Priorities | |
* Author: Weston Ruter | |
* Plugin URI: https://github.com/WordPress/gutenberg/pull/70173 | |
* Update URI: https://gist.github.com/westonruter/b4a1040de3b78fb28d56b9c8cab7da3c | |
* Gist Plugin URI: https://gist.github.com/westonruter/b4a1040de3b78fb28d56b9c8cab7da3c | |
* Primary Branch: main | |
*/ | |
namespace TryScriptModulePriorities; | |
function is_valid_fetchpriority( $priority ) { | |
return in_array( $priority, array( 'low', 'high' ), true ); | |
} | |
function enqueue() { | |
$priority_overrides = array(); | |
foreach ( array( 'foo', 'bar', 'baz' ) as $id ) { | |
if ( isset( $_GET['module_fetchpriorities'] ) && is_array( $_GET['module_fetchpriorities'] ) && isset( $_GET['module_fetchpriorities'][ $id ] ) && is_valid_fetchpriority( $_GET['module_fetchpriorities'][ $id ] ) ) { | |
$priority_overrides[ $id ] = $_GET['module_fetchpriorities'][ $id ]; | |
} | |
} | |
wp_register_script_module( | |
'foo', | |
plugins_url( 'foo.js', __FILE__ ), | |
array(), | |
null, | |
array( | |
'fetchpriority' => $priority_overrides['foo'] ?? 'low', | |
) | |
); | |
wp_register_script_module( | |
'bar', | |
plugins_url( 'bar.js', __FILE__ ), | |
array( 'foo' ), | |
null, | |
array( | |
'fetchpriority' => $priority_overrides['bar'] ?? 'low', | |
) | |
); | |
wp_register_script_module( | |
'baz', | |
plugins_url( 'baz.js', __FILE__ ), | |
array( | |
array( | |
'id' => 'bar', | |
'import' => 'static', | |
), | |
), | |
null, | |
array( | |
'fetchpriority' => $priority_overrides['baz'] ?? 'high', | |
) | |
); | |
if ( ! isset( $_GET['modules_enqueued'] ) || ! is_array( $_GET['modules_enqueued'] ) ) { | |
wp_enqueue_script_module( 'baz' ); | |
} else { | |
foreach ( array( 'foo', 'bar', 'baz' ) as $id ) { | |
if ( in_array( $id, $_GET['modules_enqueued'], true ) ) { | |
wp_enqueue_script_module( $id ); | |
} | |
} | |
} | |
} | |
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment