-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Way of determining if an asset's filename contains a hash #9038
Description
Feature request
What is the expected behavior?
I would like a way of examining each asset that's generated as part of a webpack compilation and determining whether the corresponding filename contains some flavor of hash (e.g. content hash, chunk hash, etc.—not sure how many possible hashes there are).
One way of accomplishing this might be a new boolean flag that's added to each asset, called hashInFilename or something similar.
Alternatively (less conveniently, but maybe more generally useful) it might be done by providing an property on each assets that reflects of the original filename template that was used to generate the final effective filename. For example, an asset whose final filename is 'app.abcd1234.js' might have a property filenameTemplate: 'app.[hash:8].js'. We could then infer whether a filename contains a hash or not by examining this value for one of the well-known hash substitution placeholders.
What is motivation or use case for adding/changing the behavior?
Adding a hash to a filename results in a content-addressable URL that can be safely cached indefinitely, without having to worry about adding in cache-busting URL parametres, or otherwise implementing a means of bypassing a cache.
I work on the workbox-webpack-plugin, and being able to determine whether a given asset will have a content-addressable URL will make it easier for us to generate a service worker that knows whether it's necessary to cache-bust a request for that URL.
How should this be implemented in your opinion?
Ideally, this would be some extra metadata that got unconditionally added in to each asset as part of the output of a compilation, as per the earlier suggestions.
Barring that, it would be possible to implement the same sort of thing if there were a new event that could be hooked into via a plugin, and exposed the filename template value, and the final effective name for each asset when filenames are assigned.
I am not very clear on where all the logic for this is done inside of webpack, but I'm assuming the https://github.com/webpack/webpack/blob/9ededfa92da493e750cf7d573873dcc17bb43af4/lib/MainTemplate.js and https://github.com/webpack/webpack/blob/8a7597aa6eb2eef66a8f9db3a0c49bcb96022a94/lib/TemplatedPathPlugin.js modules are closely related to this suggestion
Are you willing to work on this yourself?
yes