Nextjs Onload Function
Unanswered
Montezuma Quail posted this in #help-forum
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.
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
You can use [middleware](https://nextjs.org/docs/app/building-your-application/routing/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)