Next.js Discord

Discord Forum

Getting Global State/ Context on Server

Unanswered
British Shorthair posted this in #help-forum
Open in Discord
British ShorthairOP
Hey there. I am building a multi tenant app. The tenant can configure multiple things, such as the branding and especially the locale and timezone.

While on my API endpoints I simply use AsyncLocalStorage and on the client I could use react context, I do not see any other way to handle down the information through components in an agnostic way.

My goal is that I can have a method T(key) and localDayjs(date) that take the tenants localization settings into consideration without the requirement to actually provide it to every method call.

I want most of the page to be server rendered to benefit from SEO optimisation and especially texts should not be rendered on the client (or at least should be rendered on the server first)

How can I get the information down on the server in a way that I do not need to pass it down explicitly with the props.

Add it to the request context via a middleware and then add the request in each call?

Ideally the way would work on server AND client.

6 Replies

British ShorthairOP
Push
@British Shorthair To adjust your title first, Server is stateless there won't be any global state or context on the server
if you just meant sharing the some data on the server in different server components, just create a util function and call it in your server components
British ShorthairOP
Well, every render must happen in some kind of context/ closure, doesn’t it? Yes. After the render is done, we leave that context and it might even get destroyed. But there must be a context, otherwise I couldn’t use functions to access the request or headers.

I now have a multi tenancy based on headers (the host header effectively) and at one place per render, I want to get the tenant config and then use in all the subsequent components.
Looks like next 15 seems to be using asynclocalstorage for a request context https://github.com/vercel/next.js/pull/70573 is the underlying RequestStorage already available in 14?
British ShorthairOP
Especially for the devs: is the Fature from the linked MR meant to solve the abovementioned problem?