Skip to content

Commit

Permalink
Add support for integrity hashes when asset names contain a query string
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Sep 26, 2024
1 parent 0443f47 commit 23d32e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This is a new major version that contains several backwards-compatibility breaks

* #1344 Add options configuration callback to `Encore.enableReactPreset()` (@Kocal)

* #1345 Add support for integrity hashes when asset names contain a query string (@Kocal)

### BC Breaks

* #1321 Drop support of Node.js 19 and 21 (@Kocal)
Expand Down
9 changes: 6 additions & 3 deletions lib/webpack/entry-points-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ 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) {
// Drop query string if any
const assetNormalized = asset.includes('?') ? asset.split('?')[0] : asset;

if (assetNormalized in manifest.integrity) {
continue;
}

const filePath = path.resolve(
this.outputPath,
asset.replace(this.publicPath, ''),
assetNormalized.replace(this.publicPath, ''),
);

if (fs.existsSync(filePath)) {
Expand All @@ -115,7 +118,7 @@ class EntryPointsPlugin {
fileHashes.push(`${algorithm}-${hash.digest('base64')}`);
}

manifest.integrity[asset] = fileHashes.join(' ');
manifest.integrity[assetNormalized] = fileHashes.join(' ');
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -3123,6 +3123,35 @@ module.exports = {
done();
});
});

it('With query string versioning', (done) => {
const config = createWebpackConfig('web/build', 'dev');
config.addEntry('main', './js/no_require');
config.setPublicPath('/build');
config.addStyleEntry('styles', './css/h1_style.css');
config.enableVersioning(true);
config.configureFilenames({
js: '[name].js?v=[contenthash:16]',
css: '[name].css?v=[contenthash:16]'
});
config.enableIntegrityHashes();

testSetup.runWebpack(config, (webpackAssert) => {
const integrityData = getIntegrityData(config);
const expectedFilesWithHashes = [
'/build/runtime.js',
'/build/main.js',
'/build/styles.css',
];

expectedFilesWithHashes.forEach((file) => {
expect(integrityData[file]).to.contain('sha384-');
expect(integrityData[file]).to.have.length(71);
});

done();
});
});
});
});
});

0 comments on commit 23d32e5

Please sign in to comment.