Skip to content

Commit 15ad6a9

Browse files
committed
feature #1345 Add support for integrity hashes when asset names contain a query string (Kocal)
This PR was merged into the main branch. Discussion ---------- Add support for integrity hashes when asset names contain a query string | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- please update CHANGELOG.md file --> | Deprecations? | no <!-- please update CHANGELOG.md file --> | Issues | Fix #1269 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility. --> Commits ------- 23d32e5 Add support for integrity hashes when asset names contain a query string
2 parents 060a6e9 + 23d32e5 commit 15ad6a9

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This is a new major version that contains several backwards-compatibility breaks
88

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

11+
* #1345 Add support for integrity hashes when asset names contain a query string (@Kocal)
12+
1113
### BC Breaks
1214

1315
* #1321 Drop support of Node.js 19 and 21 (@Kocal)

lib/webpack/entry-points-plugin.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ class EntryPointsPlugin {
9595
for (const entryName in manifest.entrypoints) {
9696
for (const fileType in manifest.entrypoints[entryName]) {
9797
for (const asset of manifest.entrypoints[entryName][fileType]) {
98-
if (asset in manifest.integrity) {
98+
// Drop query string if any
99+
const assetNormalized = asset.includes('?') ? asset.split('?')[0] : asset;
100+
101+
if (assetNormalized in manifest.integrity) {
99102
continue;
100103
}
101104

102105
const filePath = path.resolve(
103106
this.outputPath,
104-
asset.replace(this.publicPath, ''),
107+
assetNormalized.replace(this.publicPath, ''),
105108
);
106109

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

118-
manifest.integrity[asset] = fileHashes.join(' ');
121+
manifest.integrity[assetNormalized] = fileHashes.join(' ');
119122
}
120123
}
121124
}

test/functional.js

+29
Original file line numberDiff line numberDiff line change
@@ -3123,6 +3123,35 @@ module.exports = {
31233123
done();
31243124
});
31253125
});
3126+
3127+
it('With query string versioning', (done) => {
3128+
const config = createWebpackConfig('web/build', 'dev');
3129+
config.addEntry('main', './js/no_require');
3130+
config.setPublicPath('/build');
3131+
config.addStyleEntry('styles', './css/h1_style.css');
3132+
config.enableVersioning(true);
3133+
config.configureFilenames({
3134+
js: '[name].js?v=[contenthash:16]',
3135+
css: '[name].css?v=[contenthash:16]'
3136+
});
3137+
config.enableIntegrityHashes();
3138+
3139+
testSetup.runWebpack(config, (webpackAssert) => {
3140+
const integrityData = getIntegrityData(config);
3141+
const expectedFilesWithHashes = [
3142+
'/build/runtime.js',
3143+
'/build/main.js',
3144+
'/build/styles.css',
3145+
];
3146+
3147+
expectedFilesWithHashes.forEach((file) => {
3148+
expect(integrityData[file]).to.contain('sha384-');
3149+
expect(integrityData[file]).to.have.length(71);
3150+
});
3151+
3152+
done();
3153+
});
3154+
});
31263155
});
31273156
});
31283157
});

0 commit comments

Comments
 (0)