-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resources with hash to be used in static web apps #1242
Comments
No idea how todo so...PR welcome... |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Would love to see the ability to detect a hash in the loadPath: "/locales/{{lng}}/{{ns}}.{{some-random-hash}}.json" Now I have "Cache-Control: no-cache", which has the downside every time the page is requested a call has to be made to the webserver to check if the content changed. With a couple of namespaces, this can have an impact on the initial load time. Cache-busting would be the ideal solution here. |
why not just loadPath: `/locales/{{lng}}/{{ns}}.${some-random-hash}.json` |
Because you don't know the ${random-hash} upfront, this gets determined at build time and is different every time the files change. When using webpack to build your application you can use it to convert your asset (e.g. image, js or json file) to an immutable asset by adding a hash to it, therefore if you have e.g. the login namespace, login.json can be converted to login."some-hash-based-on-the-content".json. Knowing the hashes upfront is therefore difficult. |
and how should i18next know about that hash? |
I think using the interpolation notation {{}} was a mistake on my part. I18next also can't know about the hash. It has to 'blindly' accept the characters that are after the dot (.) and before the .json. For example, login.1cb671935af07e6272652b8c5194d7e3.json. The hash in bold can be anything. Maybe some restrictions could be put into place like the number of characters in the hash. This could be defined by the consumer of the library. |
Ahh never mind this doesn't work 😅. I18next has to know the hash otherwise it can't fetch it from the webser. I'll come back when I have a proper answer |
They seem to have a solution here, where they calculate the hashes in javascript and match them with the hash given by webpack. I don't know how suitable this solution would be for i18next since it also requires some setup from webpack's side... Nevertheless, I am going to give this a try and see I can make it work. |
I've also encountered this issue and developed a pragmatic solution. While not optimal, it's a practical compromise that has proven effective and straightforward. My approach involves using a script to calculate a hash of the translation files before starting or building the project. This hash is then saved to a temporary file and can be easily incorporated where needed. The hash is recalculated upon each startup following any modifications. Here's an overview of the implementation: First, we have a script generate-locales-hash.js that calculates the hash:
Running npm run generate-locales-hash.js produces a locales-hash.json file:
The hash is then utilized in the i18n configuration (i18n.ts):
Finally, we integrate the hash generation into our build and start scripts in package.json:
This solution ensures that the translation files are properly versioned, mitigating caching issues while maintaining simplicity in implementation. |
🚀 Feature Proposal
I've been struggling for a few months to find the right approach to caching using i18next in combination with react.
Using a Create React App template, most examples I've found suggest to place resource files in
public/locales/en/translations.json
and to then use a plugin like i18next-http-backend to load them.However, this seems to have the flaw that if a new version of the project is published with modified translations these won't be picked up, unless you configure your server in such a way to add
no-cache, no-store
headers to all resources files in this path.This does mean that:
Looking at the output of a Create React App build, you'll normally have a bunch of generated
.js
files with hashes assigned to them. Wouldn't it make a lot more sense if there was a way to also add hashes to these translations?I know that an alternative is to use cache-busting like this:
But then you'd need to add some infrastructure to provide that
currentVersion
, which feels like setting up a work-around for a very common case.Motivation
I think i18next configuration should be kept in one place and not require configuration on the server end or exposing version numbers in the building step.
Example
I'd hope that aside from maybe turning on a configuration flag, the usage of i18next or react-i18next would remain the same.
Only the build output would change and the calls to fetch the resouces.
Note
I'm open to other suggestions for handling caching, as I might be missing something obvious.
The text was updated successfully, but these errors were encountered: