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
12 Replies
quickest-silverOP•12h 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.
blank-aquamarine•12h ago
yes it is
docs need updating here
quickest-silverOP•12h ago
the global middleware is correct?
And is there a better way to
await
an async (import messages) before CSR?blank-aquamarine•12h ago
better than what?
quickest-silverOP•12h ago
blank-aquamarine•12h ago
so you can either do this
- in getRouter (by making getRouter an isomorphic function via createIsomorphicFn)
- or in hydrate (function passed into createRouter())
quickest-silverOP•12h 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?blank-aquamarine•12h 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?
quickest-silverOP•12h 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 Manuelblank-aquamarine•12h 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 hydrate
quickest-silverOP•9h ago
the api looks so cool
Except it does this
Is that expected?
wrapping
dehydrate
in a createServerFn
fixes itblank-aquamarine•4h 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 createServerOnlyFn