localized components containing i18n tags will produce identical chunk hashes when building with --localize
🐞 Bug report
Command (mark with an x)
- [ ] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
Description
I have an application that is being translated in several (21 at the moment) languages using the Angular i18n mechanism. I'm compiling the app AOT, therefore I need a separate build for each locale. To achieve this I setup my Azure pipeline to build the app using multiple agents in parallel, one for each locale, and it works like a charm. However, this is very resource-consuming, so I'm updating to Angular 9 in order to use the --localize option, which perfectly fits my needs.
After the update I can build the app once and translate it to all languages in a few seconds, which is amazing.
I am now facing this issue: when I build my application for all locales, the resulting chunk hashes are identical. As a result, browser cache is not invalidated and files are not re-requested, so the app is not translated.
🔬 Minimal Reproduction
- clone this repo
- run
npm install - run
ng build --localize - check the dist folder: file content for
main.jsis different for each locale (template text is translated), while the hashes are the same
🌍 Your Environment
Angular CLI: 9.1.0
Node: 10.19.0
OS: win32 x64
Angular: 9.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.901.0
@angular-devkit/build-angular 0.901.0
@angular-devkit/build-optimizer 0.901.0
@angular-devkit/build-webpack 0.901.0
@angular-devkit/core 9.1.0
@angular-devkit/schematics 9.1.0
@ngtools/webpack 9.1.0
@schematics/angular 9.1.0
@schematics/update 0.901.0
rxjs 6.5.5
typescript 3.8.3
webpack 4.42.0
Anything else relevant?
In my scenario, the user language preference is stored in the cookies, and a shell app serves the corresponding folder. I can't add the locale to the URL.
Therefore in my angular.json I set baseHref="/" for all locales.
I am aware of this issue and the PR mentioned there, however the issue is still not resolved.
Duplicate of https://github.com/angular/angular-cli/issues/16526
Seems like it was supposed to be fixed with https://github.com/angular/angular-cli/pull/16817 but I am having the same issue with@angular/[email protected]
I'm also having this issue.
My repro: https://github.com/pjm0616/pastebin/tree/202004-angular-localize-outputhash
@angular and @angular-devkit version: 9.1.3
I've also included the build output in the repo.
The dist-localize folder contains the build output with localize=true, and the dist folder was built separately for each locale without localize=true.
$ npm run buildall
$ sha1sum dist{,-localize}/*/main-es5*.js
6e0407f00e27d877cb8bbcf0d7b069dfcc42cb8b dist/en/main-es5.27d2d00de67fb6cace7d.js
bc09ba481672f092a359949c6d55b14e319fc63a dist/ko/main-es5.54a148db9b0b5fb29549.js
6e0407f00e27d877cb8bbcf0d7b069dfcc42cb8b dist-localize/en/main-es5.54a148db9b0b5fb29549.js
bc09ba481672f092a359949c6d55b14e319fc63a dist-localize/ko/main-es5.54a148db9b0b5fb29549.js
#16526 was never addressed and was closed without resolution. #16817 fixes a separate issue, where the hashes would remain the same when rebuilding after changing translations and not changing code. This exact case of the file hashes being the same for different locales is still present. It was considered a regression in #16526, but has since been reclassified as a feature. Quite an annoying regression, that prevents having flat file structures.
I agree with @bridzius that #16817 fix only a symptom of the more deep issue of #16526.
I also have the same hash for multiple main.hash.js files that don't have the same content due to Angular i18n post compilation change.
(Side note, I think that, even if I'm not 100% sure, it mess with Angular service worker itself when I switch between translations of my app).
@clydin in https://github.com/angular/angular-cli/issues/16526#issuecomment-582366860 you say I quote:
Removing the use of webpack’s file hashing and moving the file name creation to a later stage of the build process is currently planned. However, such a change is out of scope within the current release candidate period.
It seems like the right things to do to fix this issue. But you didn't post any link. Can you tell where we could upvote for this solution, or even help?
(@nas90 thanks for (re-)opening this issue!)
@alan-agius4 any updates on this one?
Recently migrated to Angular 9 and faced this issue. Was really disappointing to see this. Even the different configurations for different languages (for example --configuration=production,de and --configuration=production,fr, plus properly set angular.json as Documentation advises) lead to the same hashes. This means that there is only a one way to get the flat files deployment working – to use the deprecated build parameters --i18nLocale, --i18nFile and --i18nFormat 😞
Just tried on latest Angular (10.0.5) and I'm still facing this issue 😢
This issue is still open hence it hasn’t been addressed yet.
While this is on our radar, there are issues which have a higher priority.
PRs to implement this feature are welcome.
I am having the same issue with angular 11.1.0.
I having the same issue (exactly this particular one) using Angular 11.2.11.
Is there a quick workaround to get a different hash for all locales?
also running into this issue on Angular 12.0.0. Using the --localize hash values are the same for every locale.
I think it's important to note that this occurs when we set the "baseHref": "" on each locale. We use the base href for other purposes in our application. The same build hash on each locale causes the browser to load the wrong language from browser cache instead of requesting the file from the server.
To make matters worse, location.reload(true) was deprecated making a procedural force reload difficult from the browser's side.
Our work around is to not use --localize flag with ng build. And build each language in series to force ng-build to generate a new file hash per locale. We added a build configuration for each locale.
"architect": {
...
"configurations": {
...
"en": {
"localize": [
"en"
],
"deleteOutputPath": false
},
"es-MX": {
"localize": [
"es-MX"
],
"deleteOutputPath": false
}
and some npm scripts to process each language
"build:client:Release:en": "ng build --configuration production,en",
"build:client:Release:esMx": "ng build --configuration production,es-MX",
"prebuild:client:Release": "rimraf build",
"build:client:Release": "run-s build:client:Release:*",
note: we set a non-default outputPath so adjust rimraf as needed. run-s is a utility provided by npm-run-all package.
I'm facing same issue (v12.2.2), and use different baseHref ` "sourceLocale": { "code": "fr", "baseHref": "fr" }, "locales": { "en": { "translation": "apps/profermer-web/src/i18n/messages.en.xlf", "baseHref": "en" }, "de": { "translation": "apps/profermer-web/src/i18n/messages.de.xlf", "baseHref": "de" }, ....
`
I can confirm this is still a problem. The workaround posted by @ryansheehan works, but this greatly increases build time. We have a large app with 13 different languages and migrating to Angular i18n effectively increases our build time x13. We cannot take the risk of having the same file hashes across different languages.
We have the same issue. I think this issue makes --localize unusable. We also build all languages separate.
I confirm this is still an issue in v13, the workaround to build the different locales separately can become faster using run-p of npm-run-all.
As a different workaround that doesn't involve running a separate build for each language, I wrote a shell script that adds language codes to the file hashes and their occurrences after the build finishes. This ensures uniqueness between different languages.
There is probably a more efficient way of doing this and I haven't tested it on different setups, but here it is in case it helps anyone:
Edit May 6, 2022: Improve performance and make compatible with macOS
locale-utils/fix-hashes.sh
#!/bin/bash -eu
# Appends language code to file hashes for js files
# (and references to those files) in dist/
# Motivation: https://github.com/angular/angular-cli/issues/17416
shopt -s extglob nullglob
# https://stackoverflow.com/questions/19242275/re-error-illegal-byte-sequence-on-mac-os-x
LC_CTYPE=C
# https://stackoverflow.com/a/40777793
suffixArg=()
sed --version 2>/dev/null | grep -q GNU || suffixArg=( '' )
cd ./dist/
LOCALES=($(ls))
cd "./${LOCALES[0]}"
# Find js files | extract hash | filter duplicates
# Hashes are the same for every language (this is the problem we are trying to solve)
# So we only need to do this once.
HASHES=($(find . -maxdepth 1 -name '*.js' | sed -E 's|.*\.([a-z0-9]*)\.js|\1|' | sort -u))
cd ..
for LOCALE in ${LOCALES[@]}
do
echo "Updating file hashes for $LOCALE"
cd "./${LOCALE}"
# Replace occurrences of each hash.
for HASH in ${HASHES[@]}
do
HASH_SUFFIXED=${HASH}_${LOCALE}
# echo "Replacing occurrences of hash: $HASH with $HASH_SUFFIXED"
sed -i "${suffixArg[@]}" -E "s|$HASH|$HASH_SUFFIXED|g" *.@(js|html)
done
# Rename files to update their hashes too.
for FILE in *.js*
do
# echo "Renaming $FILE"
mv "$FILE" "${FILE/.js/_${LOCALE}.js}"
done
cd ..
done
I added it as a postbuild script in package.json so it runs automatically after each build:
"postbuild": "./locale-utils/fix-hashes.sh",
I wrote a shell script that adds language codes to the file hashes and their occurrences after the build finishes. This ensures uniqueness between different languages.
I want to point out that this won't fix the problem of source maps not working properly https://github.com/angular/angular-cli/issues/16526#issuecomment-572142021
Have the same problem with Angular 13 and didn't find a workaround. No matter what I do I always end up with different main-bundles that have the same hash..
Is there is an update on this issue?
Having the same hash for different file contents is counter-intuituve, especially as this breaks cache busting, and make it impossible to do a location.reload() when the language is set by a cookie, which is the case for Firebase.
This issue makes it impossible to deploy Angular localized apps on Firebase