Run code before SSR and CSR in RC
I'm upgrading to RC from 1.120.
Prior RC, I had i18n setup with lingui similar to https://github.com/lingui/js-lingui/tree/main/examples/tanstack-start
In RC, client.ts is the same but I don't know how to inject it to
server.ts
The problem I want to solve is to call dynamicActivate (which internally imports messages and sets the active locale) before SSR and CSR
I considered global middleware at start.ts
(1/2)GitHub
js-lingui/examples/tanstack-start at main · lingui/js-lingui
🌍 📖 A readable, automated, and optimized (2 kb) internationalization for JavaScript - lingui/js-lingui
19 Replies
rare-sapphireOP•2mo ago
But the docs stats:
Global request middleware runs before every request, including both server routes and server functions.I've no idea whether SSR is included by this or not.
fascinating-indigo•2mo ago
yes it is
docs need updating here
rare-sapphireOP•2mo ago
the global middleware is correct?
And is there a better way to
await an async (import messages) before CSR?fascinating-indigo•2mo ago
better than what?
rare-sapphireOP•2mo ago
fascinating-indigo•2mo ago
so you can either do this
- in getRouter (by making getRouter an isomorphic function via createIsomorphicFn)
- or in hydrate (function passed into createRouter())
rare-sapphireOP•2mo ago
Could
getRouter be async? Is hydrate called before or after hydration? I’m asking because I read the locale from window.documentElement.lang.
The same question applies to the isomorphic fn in getRouter: how can I pass the locale from the server (extracted from the request) to the client and then use it in dynamicActivate?fascinating-indigo•2mo ago
yes getRouter can be async
hydrate is called before react hydration happens (or rather, react hydration suspends as long as router is still initializing)
The same question applies to the isomorphic fn in getRouter: how can I pass the locale from the server (extracted from the request) to the client and then use it in dynamicActivate?so you dont want to read it of window then on the client?
rare-sapphireOP•2mo ago
I read it from window because I set but If
sorry I got confused react hydration and browser loading JS.
the dom attribute will be available on
<html lang={serverLocale}> on the server, hydrate is called before react hydration, will the html attribute be available on window?window.documentElement.lang before even react hydration.
Thank you so much for your help Manuelfascinating-indigo•2mo ago
sure!
you can still send it to the client btw
you can return an object from
dehydrate (runs on the server) and then this will be passed into hydraterare-sapphireOP•2mo ago
the api looks so cool
Except it does this
Is that expected?
wrapping
dehydrate in a createServerFn fixes itfascinating-indigo•2mo ago
although dehydrate runs only on the server, it still must be isomorphic as getRouter must be isomorphic
so you can either
- supply different impls of
getRouter via createIsomorphicFn
- wrap the function you pass into dehydrate with createServerOnlyFnxenial-black•2mo ago
Hello have you solve this ?
fascinating-indigo•2mo ago
solve what? can you please create a new question including a detailed description of what you want to do?
xenial-black•2mo ago
I tried following the Lingui setup tutorial with TanStack, but it seems outdated. How can I set up Lingui with TanStack Start and enable dynamic language loading? Does TanStack support .po files?
fascinating-indigo•2mo ago
no, why would it? thats what i18n libraries/plugins are for
xenial-black•2mo ago
in lingui po file has transformation babel but I can’t find properly way to load and activate locale
xenial-black•2mo ago
https://github.com/chanphiromsok/tanstack-lingui
Now I got error hydration and still not load proper yet
GitHub
GitHub - chanphiromsok/tanstack-lingui
Contribute to chanphiromsok/tanstack-lingui development by creating an account on GitHub.
xenial-black•2mo ago
issue resolved