Browser bundle includes namespaces that are unrelated to a given page
See original GitHub issueNot sure if it’s an upstream bug or not, but I’ve noticed that the size of initialI18nStore
grows as the website is warming up. Opening any page for the first time adds dependent namespaces to the source of all other pages viewed later. If a website consists of quite a few pages, the overall size of initialI18nStore
can grow rather big and thus diminish the benefits of splitting translations into files. I’m quite sure that my projects did not suffer from this problem before I tried next-i18next
, but switching to it also meant for me the upgrading of i18next
.
Steps to reproduce
Worked both for 0.8.0 and the latest master)
cd example; yarn; yarn dev;
- Open http://localhost:3000/
- View page source, observe that
initialI18nStore
includes
{"en": {"common": ..., "footer": ...}}
- Go to http://localhost:3000/second-page and refresh the page (so that it got server-rendered)
- Return to http://localhost:3000/, refresh the page again
- View page source, observe that
initialI18nStore
includes
{"en": {"common": ..., "footer": ..., "second-page": ...}}
Expected content:
{"en": {"common": ..., "footer": ...}}
Issue Analytics
- State:
- Created 5 years ago
- Comments:34 (34 by maintainers)
Top Results From Across the Web
i18next-express-middleware should not be included ... - GitHub
I noticed that the size of my app's client bundle soared after switching to next-i18next from a custom react-i18next integration.
Read more >Introduction to Kubernetes Namespaces | SUSE Communities
Kubernetes clusters can manage large numbers of unrelated workloads concurrently ... Different namespaces can be given different policies.
Read more >Getting "type or namespace name could not be found" but ...
The problem I faced was that I created a new solution, that I added a different version of a Nuget package, than others...
Read more >ASP.NET Core breaking changes | Microsoft Learn
Lists the breaking changes in ASP.NET Core 3.0 and 3.1.
Read more >Configure Namespaces - API Gateway - TechDocs
are unrelated to those found in the namespace map of XPath-based assertions, even if they may happen to be identical.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let’s continue discussion in #54.
Happy to report that I think I’ve found a much more sane, workable solution.
Fix comes with 2386bf2.
I finally stumbled across react-tree-walker, which seems to be well-written, well-maintained, and an absolute godsend for our use case here.
Instead of creating a modified
withNamespaces
HOC and trying to keep track of a component tree’s namespace deps there (which is an inherently fraught idea), we will walk the component tree insidegetInitialProps
, as this will be request-specific, and we’re provided with the page-levelComponent
inside the first argument object, thanks to NextJs.Now that we have the
initialLanguage
and can walk a component tree based on a specific request, we can reliably produce exactly theinitialI18nStore
required to render that specific request - nothing more and nothing less.Performance is obviously the biggest concern here. During prod SSR of the example, I’m seeing walk times of 3ms, which isn’t great considering how simple a tree it is.
Hopefully in the future, a method will solving this problem will come to my attention that completely avoids the issue of re-walking/re-rendering the tree. Until now, I am quite happy with this solution.
Another added benefit is that we no longer need to maintain a custom
withNamespaces
HOC, and can simply re-export directly fromreact-i18next
.