Skip to content

Add support for integrity hashes when asset names contain a query string #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});
});
});