Make WordPress Core


Ignore:
Timestamp:
04/07/2024 11:51:57 PM (13 months ago)
Author:
peterwilsoncc
Message:

Script Loader: Improve asset concatenation Etags.

Include the asset version of JavaScript and CSS files when generating the ETag for concatenated assets in load-scripts.php and load-styles.php. This ensures the ETag is updated as script versions change (for example editor package updates) rather than only when the WordPress version changes.

The W\ prefix is added to the generated ETag to allow for CDNs and proxy servers modifying the script to add or improve the compression algorithm.

Props azaozz, dav4, ironprogrammer, johnbillion, kkmuffme, monzuralam, peterwilsoncc, sergeybiryukov.
Fixes #58433.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/load-scripts.php

    r52356 r57943  
    4646wp_default_packages_scripts( $wp_scripts );
    4747
    48 if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $wp_version ) {
     48$etag = "WP:{$wp_version};";
     49
     50foreach ( $load as $handle ) {
     51    if ( ! array_key_exists( $handle, $wp_scripts->registered ) ) {
     52        continue;
     53    }
     54
     55    $ver   = $wp_scripts->registered[ $handle ]->ver ? $wp_scripts->registered[ $handle ]->ver : $wp_version;
     56    $etag .= "{$handle}:{$ver};";
     57}
     58
     59/*
     60 * This is not intended to be cryptographically secure, just a fast way to get
     61 * a fixed length string based on the script versions. As this file does not
     62 * load the full WordPress environment, it is not possible to use the salted
     63 * wp_hash() function.
     64 */
     65$etag = 'W/"' . md5( $etag ) . '"';
     66
     67if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag ) {
    4968    header( "$protocol 304 Not Modified" );
    5069    exit;
     
    6079}
    6180
    62 header( "Etag: $wp_version" );
     81header( "Etag: $etag" );
    6382header( 'Content-Type: application/javascript; charset=UTF-8' );
    6483header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
Note: See TracChangeset for help on using the changeset viewer.