From c9656846d1dd6166c66129583928609d6464fefb Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Wed, 2 Oct 2024 08:51:08 +0200 Subject: [PATCH] Fix integrity hashes with query string, use the original assets name in "integrity" map --- CHANGELOG.md | 4 ++++ lib/webpack/entry-points-plugin.js | 7 +++++-- test/functional.js | 13 ++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d599adcc..35911a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 5.0.1 + +* #1349 Fix issue between `Encore.enableIntegrityHashes()` and filenames with a query-string (@Kocal) + ## 5.0.0 This is a new major version that contains several backwards-compatibility breaks. diff --git a/lib/webpack/entry-points-plugin.js b/lib/webpack/entry-points-plugin.js index 796ed6eb..778c9b43 100644 --- a/lib/webpack/entry-points-plugin.js +++ b/lib/webpack/entry-points-plugin.js @@ -95,9 +95,12 @@ class EntryPointsPlugin { for (const entryName in manifest.entrypoints) { for (const fileType in manifest.entrypoints[entryName]) { for (const asset of manifest.entrypoints[entryName][fileType]) { + if (asset in manifest.integrity) { + continue; + } + // Drop query string if any const assetNormalized = asset.includes('?') ? asset.split('?')[0] : asset; - if (assetNormalized in manifest.integrity) { continue; } @@ -118,7 +121,7 @@ class EntryPointsPlugin { fileHashes.push(`${algorithm}-${hash.digest('base64')}`); } - manifest.integrity[assetNormalized] = fileHashes.join(' '); + manifest.integrity[asset] = fileHashes.join(' '); } } } diff --git a/test/functional.js b/test/functional.js index 7e39eb52..725d8270 100644 --- a/test/functional.js +++ b/test/functional.js @@ -3138,11 +3138,14 @@ module.exports = { testSetup.runWebpack(config, (webpackAssert) => { const integrityData = getIntegrityData(config); - const expectedFilesWithHashes = [ - '/build/runtime.js', - '/build/main.js', - '/build/styles.css', - ]; + const expectedFilesWithHashes = Object.keys(integrityData).filter(file => { + if (!/\?v=[a-z0-9]{16}$/.test(file)) { + return false; + } + return file.startsWith('/build/runtime.js?v=') + || file.startsWith('/build/main.js?v=') + || file.startsWith('/build/styles.css?v='); + }); expectedFilesWithHashes.forEach((file) => { expect(integrityData[file]).to.contain('sha384-');