Next.js Discord

Discord Forum

Dynamic Link Routing

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
Hello, I want to create a Link to redirect my users to another url but inside the same dynamic path. Is there a way to make it without specifying the [slug] again?

- dashboard/
  |- [name]/
      |- page.tsx
      |- cool/
         |- page.tsx
      |- awesome/
         |- page.tsx


I want to create a link to change pages between cool and awesome without having to explicitly put the /dashboard/[name]

1 Reply

Holland Lop
Perhaps we might be able to help better if we better understood the restrictions you are working with (i.e. why are you trying to avoid explicitly restating the URL in question).

Potential solutions could include:
- Reading the current path name in using usePathname and programmatically rebuilding the URL via that. Note that usePathname is a client component and cannot be used server-side.
- Using headers() to figure out the current path, similarly to above, although headers is available server-side.
- You should have the dynamic path parameters available to you inside of the page/layout that you are routing from, which should include the param you would need to rebuild the URL, although this may or may not violate the restriction you mentioned in your post.