Skip to content
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

Is it safe to use reactI18nextModule on the server? #375

Closed
testerez opened this issue Jan 8, 2018 · 6 comments
Closed

Is it safe to use reactI18nextModule on the server? #375

testerez opened this issue Jan 8, 2018 · 6 comments
Labels

Comments

@testerez
Copy link

@testerez testerez commented Jan 8, 2018

I didn't find much documentation about reactI18nextModule and I was wondering if it's safe to use on the server considering that we could be rendering multiple pages for different languages at the same time.

Would you mind explaining how the instance would be tied to a specific request?

@jamuhl
Copy link
Member

@jamuhl jamuhl commented Jan 9, 2018

react-i18next does not solve the problem of having an i18n instance per request fixed to the language of that user...this is done by eg. https://github.com/i18next/i18next-express-middleware (same exists for koa and others)

also checkout the samples for next and razzle:
https://github.com/i18next/react-i18next/blob/master/example/nextjs/server.js#L8
https://github.com/i18next/react-i18next/blob/master/example/razzle-ssr/src/server.js#L11

@testerez
Copy link
Author

@testerez testerez commented Jan 9, 2018

I guess reactI18nextModule is storing the instance in some kind of global variable so it can't be used with SSR, right?

@jamuhl
Copy link
Member

@jamuhl jamuhl commented Jan 9, 2018

the reactI18nextModule is just taking the i18next instance (i18next itself) https://github.com/i18next/react-i18next/blob/master/src/context.js#L29 (basically the global i18next instance - not the one from request)

eg. for next.js:

we use initialProps: https://github.com/i18next/react-i18next/blob/master/src/context.js#L29
and get that from req: https://github.com/i18next/react-i18next/blob/master/example/nextjs/i18n.js#L38

for razzle:

has entry point for server using the one on request: https://github.com/i18next/react-i18next/blob/master/example/razzle-ssr/src/server.js#L35
and client: https://github.com/i18next/react-i18next/blob/master/example/razzle-ssr/src/client.js#L11

@testerez
Copy link
Author

@testerez testerez commented Jan 9, 2018

Ok I see, it's in fact a global: https://github.com/i18next/react-i18next/blob/master/src/context.js#L11
So even if I pass the right instance, if another request starts before the current one finishes, the instance in use will be replaced. If both requests have different languages it could be an issue.
So my conclusion is: For SSR use the provider not reactI18nextModule

If what I say is correct it could make sense to make it clear in the documentation.

@jamuhl
Copy link
Member

@jamuhl jamuhl commented Jan 9, 2018

think that makes sense...basically it's always about "injecting" the req.i18n so ssr case is rather tight coupled to i18next-express/koa-middleware

@jamuhl jamuhl closed this Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.