Route Grouping or Middleware for Redirects ?
Unanswered
Spectacled Caiman posted this in #help-forum
Spectacled CaimanOP
Im currently a new nextjs developer due to our team adopting it. I’ve done a fair amount of research on middleware, redirects and route grouping. We have a task at hand to redirect specific types of users from a few routes which all should be route grouped. I’m suggesting we use route grouping and specify a layout within that group where we can do a server action which checks for user type and then would redirect if true. My thought process was this would only make a call to check the user on the routes in question vs every route with middleware. Is this the right approach or am I thinking about this all wrong ? Thanks for reading all of this and looking forward to your feedback and suggestion. Hoping this is the right place to ask this question.
4 Replies
Devon Rex
It sounds to me like you’re asking whether you should make this check at the layout level or middleware. I believe it’s best practice not to fetch data in the layout.
Middleware would be the standard for your task, and if you want to only trigger it on certain routes you can tell the middleware what routes to run on - check out the middleware docs
Middleware would be the standard for your task, and if you want to only trigger it on certain routes you can tell the middleware what routes to run on - check out the middleware docs
Spectacled CaimanOP
Can you tell me why it’s bad practice to do a fetch in a layout. Totally separate question but feels like the right time to ask this?
@Spectacled Caiman Can you tell me why it’s bad practice to do a fetch in a layout. Totally separate question but feels like the right time to ask this?
Barbary Lion
tldr; basically it's not secure and a page could technically render before it's layout
layouts are meant to share/persist UI across pages (ie sidebar, header)