Make WordPress Core


Ignore:
Timestamp:
10/04/2025 01:49:52 AM (11 months ago)
Author:
westonruter
Message:

Emoji: Convert the emoji loader from an inline blocking script to a (deferred) script module.

This modernizes the emoji loader script by converting it from a blocking inline script with an IIFE to a script module. Using a script module improves the performance of the FCP and LCP metrics since it does not block the HTML parser. Since script modules are deferred and run immediately before DOMContentLoaded, the logic to wait until that event is also now removed. Additionally, since the script is loaded as a module, it has been modernized to use const, let, and arrow functions. The sourceURL comment is also added. See #63887.

The emoji settings are now passed via a separate script tag with a type of application/json, following the new "pull" paradigm introduced for exporting data from PHP to script modules. See #58873. The JSON data is also now encoded in a more resilient way according to #63851. When the wp-emoji-loader.js script module executes, it continues to populate the window._wpemojiSettings global for backwards-compatibility for any extensions that may be using it.

A new uglify:emoji-loader grunt task is added which ensures wp-includes/js/wp-emoji-loader.js is processed as a module and that top-level symbols are minified.

Follow-up to [60681].

Props westonruter, jonsurrell, adamsilverstein, peterwilsoncc.
See #63851, #63887.
Fixes #63842.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r60793 r60899  
    59795979
    59805980        wp_print_inline_script_tag(
    5981                 sprintf( 'window._wpemojiSettings = %s;', wp_json_encode( $settings ) ) . "\n" .
    5982                         file_get_contents( ABSPATH . WPINC . '/js/wp-emoji-loader' . wp_scripts_get_suffix() . '.js' )
     5981                wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
     5982                array(
     5983                        'id'   => 'wp-emoji-settings',
     5984                        'type' => 'application/json',
     5985                )
     5986        );
     5987
     5988        $emoji_loader_script_path = '/js/wp-emoji-loader' . wp_scripts_get_suffix() . '.js';
     5989        wp_print_inline_script_tag(
     5990                rtrim( file_get_contents( ABSPATH . WPINC . $emoji_loader_script_path ) ) . "\n" .
     5991                '//# sourceURL=' . includes_url( $emoji_loader_script_path ),
     5992                array(
     5993                        'type' => 'module',
     5994                )
    59835995        );
    59845996}
Note: See TracChangeset for help on using the changeset viewer.