angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

localized components containing i18n tags will produce identical chunk hashes when building with --localize

Open nas90 opened this issue 6 years ago • 37 comments

🐞 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.js is 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.

nas90 avatar Apr 07 '20 15:04 nas90

The way I solved this was to use a post build step to change the file names and the references. I serve with nginx and use cookies to select language. I'm considering a write-up of my solution if there is some interest.

bjorkegeek avatar May 04 '22 20:05 bjorkegeek

That would be great. I still don't quite understand though why this is not considered a bug: when building an angular app with multiple locales, the command outputs a main.js file for each locale, containing locale-specific data, so files with different content, but all these files have the same hash.

Could the locale be appended to the filename, as a quick fix, basically producing main.en.[hash].js, main.fr.[hash].js,...

kertof avatar May 04 '22 20:05 kertof

I have updated the workaround script in my previous comment to improve performance and provide support for macOS.

bartcorremans avatar May 06 '22 14:05 bartcorremans

Looking over the comments quickly, it sounds like most of the motivation here comes down to wanting to put multiple locale outputs into a single flat directory, is that the ask here? Why exactly do folks want to do this, does it make some server configuration or URL pathing easier? I think a better understanding of the motivation here would help us identify the right solution.

dgp1130 avatar May 26 '22 18:05 dgp1130

Looking over the comments quickly, it sounds like most of the motivation here comes down to wanting to put multiple locale outputs into a single flat directory, is that the ask here? Why exactly do folks want to do this, does it make some server configuration or URL pathing easier? I think a better understanding of the motivation here would help us identify the right solution.

For our projects we do path based routing in our ingress, so we opted to do localization with other methods (ie cookies). Without different hashing on the localized files, the browser cache will return the same locale even though the user switched locales, and a different index file is being served.

We dont need to flatten anything. Our server-side picks up the cookie and serves the right files.

ryansheehan avatar May 26 '22 19:05 ryansheehan

For our projects we do path based routing in our ingress, so we opted to do localization with other methods (ie cookies). Without different hashing on the localized files, the browser cache will return the same locale even though the user switched locales, and a different index file is being served.

We dont need to flatten anything. Our server-side picks up the cookie and serves the right files.

That sounds like the Cache-Control header is wrong then? If the response is contingent on a cookie which the browser doesn't key its cache by, then I think it's not safe to let the browser cache that URL.

I can see that marking those URLs as uncacheable could reduce performance for users who don't change languages, but I think that's a consequence of not putting the locale in the URL. Using a different hash is just another form of making the URL locale-dependent so the locale is taken into account in the browser cache key. Whether that's done via a locale-dependent hash, or an explicit locale path / query parameter is an implementation detail to the browser cache.

dgp1130 avatar May 26 '22 19:05 dgp1130

I don't think this is related to caching at all. You can reproduce it like explained above with just a regular "ng build --translate" - you end with 2 folders, one for each locale - so far so good!

But if you deep-dive it you notice that your main.js bundles (you have one of those for each language) is messed up. You end up with different bundles because there are differences due to language. But the HASH of all these main.ts bundles is the same.

Now depending on your solution you might end up with rewrites that (for the users browser) appear to be from the same path - that would be no problem, but since your ENGLISH-main.ts and your GERMAN-main.ts have the same hash you end up with issues when you switch the language.

Given, there may be ways to host it differently - but still: how could that NOT be a bug? The Hash of my german main.ts file is obviously based on the content of my english main.ts file. That's just wrong, it should be based on the content of my german main.ts, right?

matrium0 avatar May 26 '22 19:05 matrium0

Given, there may be ways to host it differently - but still: how could that NOT be a bug? The Hash of my german main.ts file is obviously based on the content of my english main.ts file. That's just wrong, it should be based on the content of my german main.ts, right?

As I understand it, I believe we use Webpack's [hash] replacement here, not [contenthash]. The hash value is not based on file content, it's only guaranteed to be unique within a build. This SO answer seems to have a good breakdown of the differences. @clydin is that understanding correct?

Given that generated resources shouldn't be loaded into the same page (you shouldn't be loading a French chunk into a German application), then I don't think it's necessarily wrong to see duplicate hashes across different locales.

dgp1130 avatar May 26 '22 22:05 dgp1130

Thanks for explaining and the SO link! I think I understand, but it still feels wrong and there might be some bugs with that implementation - if I build my project while only changing the german translation file the hash would still be unchanged, right? That would lead to people not getting the newest version of my german main.ts file! So basically only changing translations without touching a source file is not supported with the current build?

The question is: is there a good reason NOT to use [contenthash] or maybe add an optional flag for that? This puts a strong opinion/restriction on how files are served. For me it also seems a bit also unintuitive (because of what I believe a file-hash should do)

Example

will try to explain my approach while hosting my mini-homepage https://budisoft.at that lead to this problem:

  • on my server there is a folder de_DE and another folder en_US, containing the two localized applications
  • I set some rules on my reverse proxy (based on "Accept-Language" and a Custom Cookie) to rewrite all URLs to /de or /en_US accordingly
  • the basehref is just set to "/" - my rules ensure that the correct bundles are loaded

This was a naive - but I think reasonable - approach and would work perfectly with [contenthash], but fails with [hash] (wrong language bundle get's loaded from cache)

Solution

Thanks to your response I think I finally understand what I'd have to do instead of dirty workaround:

  • rewrite requests just for the index.html.
  • keep the baseHrefs like default, e.g. "en_US" and "de"
  • now my english index.html would request a different file location (e.g. https://budisoft/en_US/main.03b15c0004fb5dfc.js) than my german version, even when the hash is the same.

I will implement it that way, but still hope that there can be a solution that enables some flexibility while serving the bundles sometime.

matrium0 avatar May 27 '22 07:05 matrium0

Looking over the comments quickly, it sounds like most of the motivation here comes down to wanting to put multiple locale outputs into a single flat directory, is that the ask here? Why exactly do folks want to do this, does it make some server configuration or URL pathing easier? I think a better understanding of the motivation here would help us identify the right solution.

Thanks for following up! I'll try to elaborate the use case in our application, please let me know if my explanation is not clear.

Our application is typically gated by a user login. To determine the language of the application, it will look at a user's language preferences, saved in their user account. This ensures that they are served the correct language wherever they log in. If the user is not yet logged in, or the user's language is not yet set, there is logic to determine the most preferable fallback language based on the environment in which the application is deployed, as well as the user's Accept-Language list.

Once the correct language is served, we store it in the browser's cookies, ensuring that these checks and redirects do not happen again until a different user - with a different preferred language - signs in.

Because of this process, we do not use the browser URL to determine which language should be served.

This does not mean that we can't have the language in the URL - we could determine the user's preferred language and then redirect to the correct URL once we know, this is true.

However, there are other features of the application that complicate this:

  • Users can bookmark direct links within the application - or, more relevant in this case, administrators might provide their users with these bookmarks. These links should not contain language info in the URL, because they could cause the application to be served in the wrong language. We would require additional steps to detect that the user's preferred locale does not match the URL, and redirect accordingly. Unlike our current process, this is something that would have to be done every time the user accesses the bookmark.
  • Users can also share file links with one another, even with anonymous users. Similar to bookmarks, having a language code in the URL could cause the file to be served in the wrong language. Admittedly, this could probably be resolved by stripping languages codes from share links.

While none of these individual problems might be insurmountable, it makes much more sense for our use case to simply not use the browser URL for the language. It is there that we run into this issue - switching languages is unreliable when the file hashes for each language are the same, when these files are served with the same URL. We currently use the workaround I mentioned in an earlier comment of adding language codes to file hashes, but this is somewhat slow and likely to break in the future.

bartcorremans avatar May 27 '22 09:05 bartcorremans

Hey Bart, I think you are a bit in the same "mental trap" as I was. Yes, URLs to a specific content-page should not contain a language url, to ensure deep-linkability across languages. Example: https://your-page/order/123456 <= this is a link to one specific order and should work, regardless of language.

But this is only important for your own pages/routes! This doesn't matter for JavaScript bundles though. Let me explain:

If you give the link above to a german user your reverse-proxy will probably use your cookie (+ fallback) to determine the correct index.html and retrieve it from a file system like this: /opt/data/your-page/de/index.html for german OR /opt/data/your-page/en-US/index.html for english

In each index.html you have

Owner


Other Repo Issues