Next.js Discord

Discord Forum

Server components and conditional rendering

Unanswered
Argentine ant posted this in #help-forum
Open in Discord
Avatar
Argentine antOP
Hi, just wanted to check...

If you have an auth check in a page for example, and this conditionally renders out server components that should only be seen if you have the correct auth (managed in the page), do I need to do anymore inside of the server component to verify or is it safe to assume the layout has taken care of only showing that component if the auth checks out?

Thanks!

25 Replies

Avatar
@Alfonsus Ardani No you dont given that the conditions are in the same page. If you check auth in layout, then yes you would then need to check it again in page.js
Avatar
Argentine antOP
yeah i assumed so, as long as you don't import the component and render it anywhere without that check higher up
wait so what would be the difference between doing the check in a layout vs the page directly?
Avatar
@Argentine ant yeah i assumed so, as long as you don't import the component and render it anywhere without that check higher up
Avatar
you can ensure that by defining the correct props.

component A that needs auth -> Required<Session>
then set the return type of your getSession() -> Session | null

then you can only pass session to component A if you write
if(!session) notFound() / throw new Error('')
Avatar
@Argentine ant wait so what would be the difference between doing the check in a layout vs the page directly?
Avatar
layouts aren't re-run/re-rendered on soft navigation.
if you navigate from /dash/a to /dash/b,
the layout in /dash/layout,tsx wont get rerun and this leads to stale values
not to mention you can directly fetch the RSC content of /dash/b/page.tsx via postman if you dont put auth check in /dash/b
Avatar
Argentine antOP
oh yeah i see... okay thanks for that, i didn't think about that
Avatar
yes
so only put stuff in layout if its just a non-critical values or cosmetic stuff
Avatar
Argentine antOP
oh actually I just checked, i probably thought about that previously. I have a seperate layout component that isn't strictly a next.js layout, just another RSC that acts as one
Avatar
when i say layout i meant layout.tsx. Any RSC that goes in there will go stale
Avatar
Argentine antOP
oh i see, so pretty much have to do it at the page level regardless
Avatar
yeah you should be good if you have your "layout" component in page.js that handles auth stuff
basically an auth wrapper
// page.js
<Protected>
....
</Protected>
Avatar
@Alfonsus Ardani when i say `layout` i meant `layout.tsx`. Any RSC that goes in there will go stale
Avatar
Argentine antOP
what's quite weird is on one of my projects i have a sidebar component directly in layout.tsx that handles auth to render out stuff, when the cookie changes it re-renders - would you say this is expected behaviour? or is it just an issue of soft navigation
Avatar
how did you change the cookies? via client or via an api route/server acton?
Avatar
@Alfonsus Ardani how did you change the cookies? via client or via an api route/server acton?
Avatar
Argentine antOP
used a server action and also coupled with router.refresh so was a hard refresh
yeah makes sense actually
Avatar
its a hard refresh therefore it will re-run layouts
yeah
Avatar
Argentine antOP
soft navigation still the issue 😂 thanks though! probably bad practice to have that stuff in layout anyways. So just to confirm, I have my auth wrapper component, I can import my components that don’t do any other checks on their own since they’re conditionally rendered by the wrapper