Next.js Discord

Discord Forum

(ssg) with auth role based routing

Answered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
how to serve static site (ssg) with auth role based routing/rendering in nextjs app router.

probably without using middleware as server actions are not working in middleware.
Answered by B33fb0n3
You linked to the docs from the pages router. I assume you are using the app router, right?

For the app router the docs saying that you should protect it via middleware
View full answer

8 Replies

@Giant panda how to serve static site (ssg) with auth role based routing/rendering in nextjs app router. probably without using middleware as server actions are not working in middleware.
you can't, as your auth normally relies on cookies and cookies will make your page dynamic. So you need something "in front" of it. And there is only one thing, that you already have in mind: the middleware.

You can use server actions inside your middleware, as both are on the server and then the server action will be just a normal function and normal functions can run inside the middleware (on edge)

Else you can build a route handler that checks the auth in front of your page, that will be called through the middleware
i m not able to understand how then ssg work in real world applications. do you have some examples
Answer
Giant pandaOP
i m sorry for the wrong link.
i m using the app router
thanks for the clarification
solved my question
thankyou

just one more question
for conditionally rendering components based on auth
used cached data from orm server actions with unstable_cache and revalidate when needed

is this right ?
Giant pandaOP
ok, thankyou