SSR on first page request, then CSR between pages. #14641
Replies: 5 comments 8 replies
-
Any guidance on the question above, please? :) |
Beta Was this translation helpful? Give feedback.
-
Regarding CSR on other pages, can't you just use the NextAuth useSession hook inside the component body to get what you need client-side? |
Beta Was this translation helpful? Give feedback.
-
I guess I'm asking for a feature that doesn't exist? Render first page request for the entire app SS, then subsequent requests are CSR. |
Beta Was this translation helpful? Give feedback.
-
I've also run into this same scenario. I'm not sure what the solution is, or what I want as a solution if there is none... I suppose it would be going back to In my case, I'm using SSG pages with client-side authorization so I don't have to SSR every request (which leads to slower pages as they can't be cached). I would rather redirect if it's an invalid session for the very first request before the page is served. I can use Maybe there could be a way to opt into some server side page handler that doesn't affect the content of the page, so they can still be SSG? Basically |
Beta Was this translation helpful? Give feedback.
-
Maybe I'm missing something, but I don't see a reason you can't cache SSR pages. You just have to implement that functionality yourself and build a client-side data cache. You won't be able to mix SSG with other options as you mentioned, but you could viably use SSR to do your auth/redirect, and then write up a small client-side cache to store the data. Then in your SSR you could check for the cache (via a cookie or similar) and if it exists, just use that cached data. That flow shouldn't eat up much page load time, and you won't have to deal with client-side redirects. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Libraries versions
"next": "^9.4.4"
"next-auth": "^2.0.0"
Hi,
Is there a way to get User Session on first page request as SSR, but subsequent requests to other pages would run CSR since I can store session in memory?
Right now I have to implement
getServerSideProps
on all logged-in pages. Which makes my entire app SSR and routing between pages become slower even though most pages don't do any data fetching ingetServerSideProps
Here's a sample of what my
getServerSideProps
look like:My CSR hack right now is to get the session inside
_app
, and check on thepageProps
if it'sisProtected
. Which means the initial page request UX is a bit nasty because until I get session or undefined, I don't know what to show to the user. Also if there is no session, there'd be a heck up before I can redirect them to login.Is this a current limitation of NextJS or is there a way to achieve my dream?
Beta Was this translation helpful? Give feedback.
All reactions