Skip to content

Commit

Permalink
Merge pull request #45 from humanmade/use-chunkhash-as-version-number
Browse files Browse the repository at this point in the history
Use hash in filename as the asset version number
  • Loading branch information
goldenapples authored Jul 26, 2022
2 parents 9fdb74d + 0dc598f commit 2e61950
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.6.2

- Ensure that a version string is always set on an asset, even when the built file has a hash string in the file name. This ensures proper cache invalidation in sites using asset concatenation.

## v0.6.1

- Fix get_file_uri() when using symlinks

## v0.6.0

- **Breaking**: Remove deprecated `autoregister`, `autoenqueue`, and `register_assets` methods.
Expand Down
6 changes: 3 additions & 3 deletions inc/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ function get_version( string $asset_uri, string $manifest_path ) : ?string {
// but numbers and the letters a through f", which matches most common hash
// algorithms (including Webpack's default of MD4) while rarely matching
// any human-readable naming scheme.
if ( preg_match( '/[a-f0-9]{16}/', $asset_uri ) ) {
// If the file is already hashed then a version string is not required.
return null;
if ( preg_match( '/[a-f0-9]{16,}/', $asset_uri, $possible_hash ) ) {
// If the file is already hashed, then use the existing hash as the version string.
return $possible_hash[0];
}

// Next, try hashing the contents of the asset manifest file (if available).
Expand Down
6 changes: 3 additions & 3 deletions tests/inc/class-test-asset-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function provide_script_asset_cases() : array {
'handle' => 'custom-handle',
'src' => 'https://my.theme/uri/fixtures/editor.03bfa96fd1c694ca18b3.js',
'deps' => [ 'wp-data' ],
'ver' => null,
'ver' => '03bfa96fd1c694ca18b3',
],
],
'development script' => [
Expand Down Expand Up @@ -212,7 +212,7 @@ public function provide_style_asset_cases() : array {
'handle' => 'frontend-styles',
'src' => 'https://my.theme/uri/fixtures/frontend-styles.96a500e3dd1eb671f25e.css',
'deps' => [ 'dependency-style' ],
'ver' => null,
'ver' => '96a500e3dd1eb671f25e',
],
],
'production stylesheet with no explicit handle' => [
Expand All @@ -225,7 +225,7 @@ public function provide_style_asset_cases() : array {
'handle' => 'frontend-styles.css',
'src' => 'https://my.theme/uri/fixtures/frontend-styles.96a500e3dd1eb671f25e.css',
'deps' => [ 'dependency-style' ],
'ver' => null,
'ver' => '96a500e3dd1eb671f25e',
],
],
'production stylesheet not in manifest' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/inc/class-test-manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function provide_get_version_cases() : array {
'hashed asset filename' => [
'main.03bfa96fd1c694ca18b3.js',
dirname( __DIR__ ) . '/fixtures/prod-asset-manifest.json',
null,
'Version should be "null" if asset is determined to contain a hash already',
'03bfa96fd1c694ca18b3',
'Version should be set to the hash in the filename if asset is determined to contain a hash already',
],
'fall back to manifest content hash' => [
'main.js',
Expand Down

0 comments on commit 2e61950

Please sign in to comment.