get current URL in layout
Unanswered
Tomistoma posted this in #help-forum
TomistomaOP
Is it possible to get the current URL in the
Is this possible, or am I just doing it wrong and should I stick to doing it in middleware?
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?
1 Reply
@Tomistoma 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?
Transvaal lion
I think moving to the layout file os a bad ideea since. If the checks were in the middleware the page didn't even load you would've got redirected. If you do it with the layout you might get a blank page for a sec then redirect. That is not nice for the user. What is you current middleware implementation?