Next.js Discord

Discord Forum

If my context not depending on the client, is it still advisable to use useContext?

Answered
Aplomado Falcon posted this in #help-forum
Open in Discord
Aplomado FalconOP
I'm making a multilingual site and the current locale is known from the url path (e.g. /en/). How can I access the current locale (and possibly translations) in a component deep down? I'm afraid that useContext will prevent the page from being able to be prerendered fully on the server.
Answered by Eric Burel
Data can be shared between RSCs using "cache"
View full answer

24 Replies

Aplomado FalconOP
thanks a lot! I will try that
Data can be shared between RSCs using "cache"
Answer
useContext won't prevent a server prerender though
a client component can be prerendered on the server and will be then hydrated on the client
here usePathname is indeed more direct though it basically does the same already
but you have to make the component a client component (most often it's fine because deeply nested component are usually client components)
Aplomado FalconOP
Thanks for the references! I will read a bit more into it so that I understand it
Aplomado FalconOP
Thanks for the insightful articles! I really like the first one about advanced nextjs server context. @Eric Burel
It sounds nice to use Reacts cache function to cache the translations per locale and then get them in the components on the server. But then I get the problem that I don't know how to get the path name on the server and only can get it with usePathname on the client. Maybe that makes sense because a component could be rendered on multiple paths..
but the server-only-context trick (the github link from manvalls) normally allow to set the route params in the cache and get it from nested components
I am pretty sure Next will include solutions to do that in the future, there has been work around contexts internally
and accessing the URL is a subject that leads to heated discussion on GitHub
Next often takes time to solve this but when they do they provide sleek solutions, in the meantime "cache" does the job
again you don't usually have a super deep RSC tree in most apps yet
Aplomado FalconOP
Thanks! Yea there is a lot of development on it I guess. I used a set-up loosely based on this blog article now for i18n: https://carlogino.com/blog/nextjs13-i18n
I'm struggling with using the next locale settings though. It switched from the /pages directory to the /app directory and on the pages directory everything was fine but on the app directory I get 404's for different languages. NextJs documentation on i18n seems unchanged and doesn't mention differences between these two approaches. I think for now I will not use the i18n configuration parameter and just set i18n based on path parameter.
Aplomado FalconOP
Thanks, that clears things up 🙂 really clear code