Next.js Discord

Discord Forum

Best practices for storing user object "globally" in app router.

Unanswered
Cuban Crocodile posted this in #help-forum
Open in Discord
Cuban CrocodileOP
Hello!

I am building an app with supabase which has a library that fetches the user on the server (or the client). I'm a bit newer to the SSR world and still want to do things SPA like. I would like to put the user object into a context or an SWR cache and consume that anywhere else in the app. What are best practices for doing something like this with Next 13?

Thanks for your time

2 Replies

It’s quite common to use Context API in Next.js.

A preferred way is to pass the user object from a server component, and in the client component, you can use Context API or libraries like SWR as you wish, receive the user object via props, and pass it to context provider. Then, you can consume the context in any client component under the provider.

For SWR, you can also read this: https://swr.vercel.app/docs/with-nextjs#pre-rendering-with-default-data
Cuban CrocodileOP
thanks, I think either of those options will be perfect!