27

I'm using Next js. I want to use dynamic routing based on this documentation. How can I set two parameters like href="/page/[p1]/[p2]" to the Link component? What should be the structure of my page file and how use the router.query?

4
  • What have you tried so far? Commented Sep 24, 2019 at 8:11
  • I used a wildcard and a query string like page/[p1]?p2=... , but I want use 2 wildcard. Also use this structure for my page name [p1][p2].tsx Commented Sep 24, 2019 at 8:16
  • 1
    Check this github.com/zeit/next.js/tree/canary/examples/dynamic-routing Commented Sep 24, 2019 at 8:23
  • Thanks, Its a good example Commented Sep 24, 2019 at 8:29

5 Answers 5

36

Folder structure should be Like that

Page - Folder 
 blog - Folder Name
 [p1] - Folder Name
   [p2].js - File Name

it will work when you call URL like /blog/postname/id it will call p2.js page

Sign up to request clarification or add additional context in comments.

1 Comment

You can also get the details back in your [p2.js] file by const { query: { p1, p2 }, } = req; console.log("P1="+p1); console.log("P2="+p2);
25

You can do this with the help of dynamic routing in nextjs 9. For example, pages/post/[postId]/[commentId].js would match /post/p1/c1. Its query object would be: { postId: 'p1', commentId: 'c1' }. your Link component should be like this:

<Link
   href="/post/[postId]/[commentId]"
   as={`/post/${postId}/${commentId}`}>
      <a>link to comment</a>
</Link>

3 Comments

What would the file structure in the pages directory look like when doing this?
@P4nd4b0b3r1n0 pages/post/[postId]/[commentId].js you need a folder with the name of first parameter : [postId] and the js file with the name of second one : [commentId].js
I was wondering if I should use both [postId] folder and [postId].js file if I wanted to handle both top-level and bottom-level pages, and indeed it works like that
3

The folder structure in your nextjs projects should look like this

📂user
 ┗ 📂[user-id]
   ┗ 📂 posts
      ┗ [post-id]

this will generate url paths for you like below, which is widely used format

/users/[id]/posts/[postId]

You can follow these guidelines to structure your urls: https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/#h-nesting-resources-for-hierarchical-objects

In summary, formats like "/pages/p1/p2", do not really tell a whole lot of information what sub-resources you are choosing and are considered less readable.

Comments

2

Other method is create a slug [...slug] and get the parameters with router.query.

dynamic-routes#catch-all-routes

1 Comment

Igor Chaves, a link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
1

Folder structure like this will work

-[blog] //folder name
 -[post].js //file name
-items //folder name
 -[category] //folder name
  -[subCategory].js //file name

you can simply call the blog and post like

<a href="/blog/post"></a>

you can simply call the category and subcategory like

<a href="/items/category/subCategory"></a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.