• So this is in relation to the move to using modules and importmap.

    we had a persistent, although intermittent error of:

    “The specifier “@wordpress/interactivity” Relative module specifiers must start with “./”, “../” or “/”

    Looking at the code the wordpress core importmap declaration is made via WP_Script_Modules with an add_action in wp_head which causes it to load after plugins enqueue scripts
    As a result we encountered issues with the declaration occuring after modules need to be loaded. We found that making the declaration using wp_print_scripts we could add the importmap code before enqueue scripts fired and this resolved the error although now we have two script tags for

    The issue of the “@wordpress/interactivity” Relative module specifiers” was intermittent and buggy and presumably down to plugins (we were running this with woocommerce and think that may have been the issue.

    has anybody else had this issue? the fix we have seems a bit dodgy although it seems to work. If anybody else has suggestions as to how best to debug this error for a more robust fix or a way to remove the initial importmap declaration that would also be much appreciated.

    Please ELI5

Viewing 2 replies - 1 through 2 (of 2 total)
  • Some web browsers have trouble getting anything from importmap if there’s a whole bunch of other javascript before this:

    <script type="importmap" id="wp-importmap">

    Those web browsers will throw an error like this:

    Uncaught TypeError: The specifier “@wordpress/interactivity” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”.

    In the file wp-includes/class-wp-script-modules.php, there is a public function called add_hooks()

    The trouble is, there is no priority set in the following add_action in that function:

    		add_action( $position, array( $this, 'print_import_map' ) );
    add_action( $position, array( $this, 'print_enqueued_script_modules' ) );
    add_action( $position, array( $this, 'print_script_module_preloads' ) );

    That pretty much leaves it up to chance whether the import map loads before or after other javascript.

    So to make sure it loads before most other javascript, I added this to my child theme’s functions.php file:

    remove_action( 'after_setup_theme', array( wp_script_modules(), 'add_hooks' ) );
    add_action('wp_head', 'print_import_map_before_plugin_scripts', 8);
    function print_import_map_before_plugin_scripts() {
    wp_script_modules()->print_import_map();
    wp_script_modules()->print_enqueued_script_modules();
    wp_script_modules()->print_script_module_preloads();
    echo "\r\n";
    }

    Maybe the WordPress team will add priority to those actions in the wp-includes/class-wp-script-modules.php file eventually. If that happens, it will no longer be necessary to add all this stuff to the functions.php file. But for now, this works, and it gives you just one importmap script instead of two.

    Just to make this thread easier to find for anyone searching for a solution to the same problem I ran into – I came across this issue on a website with the Twenty Twenty-Two theme.

    I noticed it when the hamburger menu button just seemed to stop working sporadically on an older browser (Firefox 115.19.0esr). I would click on the toggle and nothing would happen. The menu won’t open. Oddly enough, this is not an issue on an even older browser (Chrome 103.0.5060.134).

    Other websites running older WordPress themes (such as Twenty Twenty-One) had no menu issues on even these old browsers. So I thought it could be a block theme issue. But it wasn’t.

    When I was trying to figure this out, I found a lot of threads with people saying it could be a plugin conflict. It does contribute to the issue if you have a lot of plugins (the more plugins you have, the more scripts could get in front of the importmap script), but it’s not the fault of any particular plugin.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘importmap @wordpress/interactivity’ is closed to new replies.