Make WordPress Core

Changeset 57943


Ignore:
Timestamp:
04/07/2024 11:51:57 PM (6 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.

Location:
trunk/src/wp-admin
Files:
2 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' );
  • trunk/src/wp-admin/load-styles.php

    r55994 r57943  
    4949wp_default_styles( $wp_styles );
    5050
    51 if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $wp_version ) {
     51$etag = "WP:{$wp_version};";
     52
     53foreach ( $load as $handle ) {
     54    if ( ! array_key_exists( $handle, $wp_styles->registered ) ) {
     55        continue;
     56    }
     57
     58    $ver   = $wp_styles->registered[ $handle ]->ver ? $wp_styles->registered[ $handle ]->ver : $wp_version;
     59    $etag .= "{$handle}:{$ver};";
     60}
     61
     62/*
     63 * This is not intended to be cryptographically secure, just a fast way to get
     64 * a fixed length string based on the script versions. As this file does not
     65 * load the full WordPress environment, it is not possible to use the salted
     66 * wp_hash() function.
     67 */
     68$etag = 'W/"' . md5( $etag ) . '"';
     69
     70if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) && stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) === $etag ) {
    5271    header( "$protocol 304 Not Modified" );
    5372    exit;
     
    85104}
    86105
    87 header( "Etag: $wp_version" );
     106header( "Etag: $etag" );
    88107header( 'Content-Type: text/css; charset=UTF-8' );
    89108header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
Note: See TracChangeset for help on using the changeset viewer.