tooling.report

feature

Hash ignores parent directories

Are URL hashes resilient to path changes?

Person in shorts with blue hair walking left

Introduction

Generating hashes for URLs based on file paths can result in unintentional cache invalidation when directory paths change between builds. This is commonly seen in Continuous Integration workflows that perform builds in a different directory on each run, or due to otherwise unrelated configuration changes.

The Test

This test runs a build twice using the same configuration and source, but with a differing path to the entry module (src1/index.js, then src2/index.js).

index.js

(async function() {
  const { logCaps } = await import('./utils.js');
  logCaps('This is index');
})();

utils.js

export function logCaps(msg) {
  console.log(msg.toUpperCase());
}

The result of both builds should be functionally identical, which means both builds should produce the same hashed URLs.

Conclusion

browserify

Gulp-based hashing solutions like gulp-rev-all generally calculate a file's hash using its contents. As long as hashing is performed last, the generated URLs are based on final source only and not affected by parent directories.

parcel
rollup

The files get different hashes despite having identical contents.

Issues

webpack

Webpack's [contenthash] hashing is based on the file contents, which means hashes only change when the resulting output can differ. Directory names are not included in the file's contents, so they do not affect its hash.