Next.js Discord

Discord Forum

Nextjs Onload Function

Montezuma Quail posted this in #help-forum
Open in Discord
Montezuma QuailOP
I was trying to check whether an user has an auth token in cookies on load. And if it is there I was passing down that token to a client component to change client auth state as it was required to perform some other UX based things.

Now, in my approach I was doing it in layout.tsx but when built the app it showed now everypage is by default using server side rendering. So, my question is there a better way to this above mentioned stuff like moving it into some other component and just making the pages statically render reducing the server cost. Or is it the only way to do this stuff and if so is there any performance hits that I would face or any cost related hits in production. Also if there is a better way to do this please mention?

Thanks in advance.

1 Reply

fuma 💙 joulev
You can use [middleware]( to redirect users if they're not logged in or unauthorised. You are able to read cookies and redirect users without making your page SSR (keep it at SSG)