Avoiding duplicate async function calls
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
Is there a way to pass props to a server page component from the layout? I have an asynchronous authentication function that parses a session token, makes DB call, and returns account/session data, but it's needed in both the layout (to be passed to shared components between all pages like sidebars and footers) and in the main page content. Is there a way to avoid calling the function twice? They're both server components so I don't believe React contexts are applicable here.
I'm new to Next.js & React so pardon me if this is a silly question.
I'm new to Next.js & React so pardon me if this is a silly question.
3 Replies
you can use [cache](https://react.dev/reference/react/cache) to run the auth function only once and share the results with multiple server components, it is scoped per request
@Rafael Almeida you can use [cache](https://react.dev/reference/react/cache) to run the auth function only once and share the results with multiple server components, it is scoped per request
Sun bearOP
Interesting, I think that should solve my issue, thanks!
How is it scoped per request by the way? Just curious.
How is it scoped per request by the way? Just curious.
Sun bearOP
Also is there a way to revalidate it? Even after removing the session cookie it still returns the cached value