Skip to content

Commit

Permalink
feature #1345 Add support for integrity hashes when asset names conta…
Browse files Browse the repository at this point in the history
…in 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
  • Loading branch information
Kocal committed Sep 28, 2024
2 parents 060a6e9 + 23d32e5 commit 15ad6a9
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 15ad6a9

Please sign in to comment.