Next.js Discord

Discord Forum

access route path in server

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
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 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 /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
In my scenario would it be better to use a middleware?
Transvaal lionOP
I don't think that would work in my case because this is the example URL path

/user/profile-setup

if the user visits /user The layout here is where i perform the redirect

This /user/profile-setup path would always trigger the /user layout wouldn't it?

This is my current route group.

user/(protected)/profile-setup/page.tsx
user/(protected)/page.tsx
user/(protected)/layout.tsx
Unless 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.tsx
this 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 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