Next.js Discord

Discord Forum

get current URL in layout

Unanswered
Tomistoma posted this in #help-forum
Open in Discord
TomistomaOP
Is it possible to get the current URL in the layout.tsx file? Basically, currently I have a middleware that is checking if a user is authorized to go to a certain URL, and if not, redirect them. I felt this wasn't quite right to be in the middleware, as just a bit ugly and hard to manage with lots of different pages and rules, so I thought I'd move the logic to each layout.tsx individually, as they all still run on the server. However, in one particular case, I can't just do a normal redirect, I have to replace part of the existing URL (I am changing a slug value for context) and redirect to that new url, so I need to get the URL in the layout.

Is this possible, or am I just doing it wrong and should I stick to doing it in middleware?

0 Replies