access route path in server
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Is there a way to access the current route/path on the server layout without converting it into a client component?
16 Replies
Transvaal lionOP
What am i supposed to extract out from the headers? My headers doesn't expose a pathname unless i need to explicitly set it on every route
@Transvaal lion What am i supposed to extract out from the headers? My headers doesn't expose a pathname unless i need to explicitly set it on every route
use middleware for example in https://github.com/vercel/next.js/issues/47108#issuecomment-1480431453, then use headers() to read it. But I will repeat that it doesn't do what you expect it to do
Transvaal lionOP
Just to clarify my reason why i need to access the path.
I currently have a protected group layout that checks a value from my returned API.
If the value returns true i need to redirect the user to example
However currently I have this error
My speculation would be becuase it is attempting to redirect to /profile-setup even thought it is already on that url
I currently have a protected group layout that checks a value from my returned API.
If the value returns true i need to redirect the user to example
/profile-setup using the redirect functionHowever currently I have this error
Too many calls to Location or History APIs within a short timeframe.My speculation would be becuase it is attempting to redirect to /profile-setup even thought it is already on that url
In my scenario would it be better to use a middleware?
@Transvaal lion Just to clarify my reason why i need to access the path.
I currently have a protected group layout that checks a value from my returned API.
If the value returns true i need to redirect the user to example `/profile-setup` using the `redirect` function
However currently I have this error
`Too many calls to Location or History APIs within a short timeframe.`
My speculation would be becuase it is attempting to redirect to /profile-setup even thought it is already on that url
i suggest making a deeper route group where the layout doesn't cover /profile-setup
Transvaal lionOP
I don't think that would work in my case because this is the example URL path
if the user visits
This
This is my current route group.
/user/profile-setupif the user visits
/user The layout here is where i perform the redirectThis
/user/profile-setup path would always trigger the /user layout wouldn't it?This is my current route group.
user/(protected)/profile-setup/page.tsxuser/(protected)/page.tsx user/(protected)/layout.tsxUnless i move profile-setup  outside protected group
or, wait a min
app/
user/
(protected)/
layout.tsx <- auth logic here, NOT redirection logic
profile-setup/
page.tsx
(not-setup)/
layout.tsx <- redirection logic
page.tsxthis is better
there are plenty of ways to design this
it doesn't make sense to access the pathname in a layout and that is a hard restriction
@Transvaal lion Just to clarify my reason why i need to access the path.
I currently have a protected group layout that checks a value from my returned API.
If the value returns true i need to redirect the user to example `/profile-setup` using the `redirect` function
However currently I have this error
`Too many calls to Location or History APIs within a short timeframe.`
My speculation would be becuase it is attempting to redirect to /profile-setup even thought it is already on that url
for this, if you check for pathname in the layout, this can happen
* user goes to
* redirected to
* user does not finish setup but click whatever link to go to
* the layout is not run again so
* user goes to
/user* redirected to
/user/setup* user does not finish setup but click whatever link to go to
/user again* the layout is not run again so

Transvaal lionOP
Hmm let me take a minute to process this and try your suggested route group. Thanks a bunch @joulev
i removed my first suggestion because it was bad, use the remaining structure suggestion