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-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.