Next.js Discord

Discord Forum

Does wrapping the Root Layout content with the Next-Auth provider make everything client-side?

Answered
Western thatching ant posted this in #help-forum
Open in Discord
Western thatching antOP
I've read a lot of conflicting information about how to set up the Next-Auth provider with the app directory in a way that allows both getServerSession() on the server side components, and useSession() on the client side components.

This is the setup:

src/app/provider.tsx (image 1)
src/app/layout.tsx (image 2)

Does this setup make it so that everything is rendered on the client side? And if so, how should one properly set up the context provider to ensure authentication on the server side?
Answered by American Crocodile
Server components can be nested inside any client component, but they may not be able to see context provided by the client component.
This includes a React Query / Auth provider.

Please note that "client components" also run on the server, they just also ship JS instead of pure HTML to the client and your usual hooks work with them.
View full answer

6 Replies

@joulev > Does this setup make it so that everything is rendered on the client side? no.
Western thatching antOP
Would this also be the case for use with a React Query provider?

In the case of wrapping RSCs with Client side context providers or other client side components with children, how does that work when rendering on the page? Does the provider come after the initial content during streaming, or does the server just send the client component to the browser and tell it to wrap the RSCs inside of it? Just in terms of how to optimize it for fast loading
American Crocodile
Server components can be nested inside any client component, but they may not be able to see context provided by the client component.
This includes a React Query / Auth provider.

Please note that "client components" also run on the server, they just also ship JS instead of pure HTML to the client and your usual hooks work with them.
Answer
@American Crocodile Server components can be nested inside any client component, but they may not be able to see context provided by the client component. This includes a React Query / Auth provider. Please note that "client components" also run on the server, they just also ship JS instead of pure HTML to the client and your usual hooks work with them.
Western thatching antOP
Ah, that's fantastic! So if everything is wrapped in a client side provider, all the client side components down in the hierarchy can use the context from the provider, but the server components cannot because they are pre-rendered?
American Crocodile
Yes, essentially. Some frameworks use black magic (cookies for example) to share state with server components as well. It's not always clear which frameworks do this
Western thatching antOP
Gotcha, right.. with server+client "hybrid" providers I'd assume.

That's awesome! Makes everything much clearer.