Angular 6生产构建无法生成唯一哈希问题求助
The Problem
I was working on an Angular 6.0.3 project where production builds weren't generating unique hashes for bundled files—even after modifying TypeScript or HTML content, the hash on what I thought was the main bundle (script.js) never changed. I couldn't reproduce this issue in a fresh Angular CLI project, so I knew the problem was specific to my existing project.
Here's the relevant production build configuration from my angular.json:
{ "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "configurations": { "production": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true, "baseHref": "/", "serviceWorker": true } } } } }
I ran these commands repeatedly with no visible change to the script.js hash:
ng build --aot --output-hashing=allng build --output-hashing=all
The Misdiagnosis
At first, I blamed Angular's build pipeline—since the hash wasn't updating, I assumed the bundler wasn't detecting file changes. But after inspecting the full build output more carefully, I realized I'd made a critical mistake: the actual compiled application code lives in main.js, not script.js. The script.js file was just a tiny bootstrap loader that rarely changes, which explained why its hash stayed the same regardless of my app code edits. Angular was actually generating unique hashes for main.js correctly all along.
The Real Root Cause
Once I confirmed Angular's build process was working as expected, I shifted focus to deployment. I was hosting the app on Firebase, and the real issue turned out to be Firebase's caching rules. Even after disabling these rules, returning users were still loading old cached versions of the bundled files from their browsers or Firebase's edge caches.
The Fix
I tried multiple cache-clearing workarounds within the existing Firebase project, but none resolved the issue for all users. The final solution was to create a new Firebase project and deploy the updated app there. This completely bypassed all existing cached content, ensuring every user loaded the latest version with the correct hashed bundles.
内容的提问来源于stack exchange,提问作者Sameer

