Make WordPress Core

Changeset 49865


Ignore:
Timestamp:
12/22/2020 03:42:14 PM (4 years ago)
Author:
desrosj
Message:

Twenty Twenty-One: Only load IE specific polyfills when actually using Internet Exploreer.

The twenty-twenty-one-ie11-polyfills script now has a null source, and the new twenty-twenty-one-ie11-polyfills-asset (which points to the actual polyfills.js source) will be loaded only if IE is detected by through the use of wp_get_script_polyfill().

Because the original script name remains the same, this change is backwards compatible with any code registering twenty-twenty-one-id11-polyfills as a script dependency.

Props poena, ismail.elkorchi, peterwilsoncc
Fixes #52098.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentytwentyone/functions.php

    r49826 r49865  
    397397    // Note, the is_IE global variable is defined by WordPress and is used
    398398    // to detect if the current browser is internet explorer.
    399     global $is_IE;
     399    global $is_IE, $wp_scripts;
    400400    if ( $is_IE ) {
    401401        // If IE 11 or below, use a flattened stylesheet with static values replacing CSS Variables.
     
    417417    }
    418418
     419    // Register the IE11 polyfill file.
    419420    wp_register_script(
    420         'twenty-twenty-one-ie11-polyfills',
     421        'twenty-twenty-one-ie11-polyfills-asset',
    421422        get_template_directory_uri() . '/assets/js/polyfills.js',
    422423        array(),
    423424        wp_get_theme()->get( 'Version' ),
    424425        true
     426    );
     427
     428    // Register the IE11 polyfill loader.
     429    wp_register_script(
     430        'twenty-twenty-one-ie11-polyfills',
     431        null,
     432        array(),
     433        wp_get_theme()->get( 'Version' ),
     434        true
     435    );
     436    wp_add_inline_script(
     437        'twenty-twenty-one-ie11-polyfills',
     438        wp_get_script_polyfill(
     439            $wp_scripts,
     440            array(
     441                'Element.prototype.matches && Element.prototype.closest && window.NodeList && NodeList.prototype.forEach' => 'twenty-twenty-one-ie11-polyfills-asset',
     442            )
     443        )
    425444    );
    426445
Note: See TracChangeset for help on using the changeset viewer.