CDN version pinning and hash drift — why a hash breaks after deploy
The most common broken hash is not a mistake in computation; it is a deployed file that no longer matches the bytes you hashed.
How drift happens
An unversioned URL such as /lib/latest/app.js changes content when the CDN publishes a new version. The hash does not, so the next visit is blocked.
A pipeline that minifies right before deploy, or a server that transcodes the charset and rewrites line endings, causes the same failure: the transformation happens between hashing and serving.
Put the content in the URL
The simplest prevention is a content hash in the file name or path. When app.7f3a91.js is named after its bytes, caches can live long while the content stays fixed.
The SRI digest and the file-name hash are the same idea, so have the build compute once and use it for both; there is then little room for them to disagree.
Compute it in the pipeline
Hash the final artefact, not the source. A shell example:
openssl dgst -sha384 -binary dist/app.js | openssl base64 -A
# put the result straight into integrity="sha384-..."
Check it after deploy
Add a step that compares the hash written into the HTML with the hash of the file actually served. That catches drift at deploy time instead of in a user's browser.
To confirm in the browser, open the network panel for the response size and headers, and compare the 'computed SHA-384 integrity' value from the console error with your own.
Caching and SRI
A browser does not re-run the integrity check on a resource it already has. A cached response is reused without verification, so the moment of verification is the first load.
That is another reason to put the content in the URL for long-cached files: when the content changes, the URL changes with it.
Questions
Does SRI protect me from a CDN outage?
No. It blocks a response whose content differs; availability when nothing is returned is a separate problem. If you use multiple CDNs, design the fallback order as well.
How do I keep the hash updated automatically?
A build plugin or deploy script reads the final file, generates the integrity value and injects it into the HTML template. That is the usual approach.
Sources
Every example here can be pasted into the workbench as it is.
Open the workbench