24

I carefully read the docs of next routing system. It only mentions that I could achieve dynamic routing like this:

http://localhost:3000/level1/dynamicSlug

But I am trying to achive something like this:

http://localhost:3000/level1/level2/dynamicSlug

And I want level2 to be created dynamic too.

5
  • you can simply create the folder structure like level1 -> level2 in page folder Commented Aug 25, 2019 at 18:40
  • @Wasif thanks you very much, but what I should do if I want that "level2" is dynamic, sorry my English Commented Aug 25, 2019 at 18:42
  • what do you want to achieve localhost:3000/level1/dynamicSlug1/dynamicSlug2? Commented Aug 25, 2019 at 18:45
  • @Wasif I want to have that level2 also dynamic, not by simply create a level2 folder, because data from backand reuqires level2 to be create dynamic Commented Aug 25, 2019 at 18:47
  • you can check out this github.com/zeit/next.js#custom-server-and-routing hope that helps Commented Aug 25, 2019 at 18:58

5 Answers 5

30

It is possible to do nested scenarios according to your request in this way. for example:

pages/
  level1/
    [dynamicSlug]/
      - index.js           // will match for /level1/1234
    level2/
      - index.js           // will match for /level1/level2
      - [dynamicSlug].js   // will match for /level1/level2/1234

Or

pages/
  level1/
    [dynamicSlug]/
      - index.js           // will match for /level1/1234
    level2/
      - index.js           // will match for /level1/level2
      [dynamicSlug]/
        - index.js         // will match for /level1/level2/1234
Sign up to request clarification or add additional context in comments.

1 Comment

I believe my question is a follow up to this. If you could give it a look I'd be forever grateful
5

I know this is a bit of an old post, but I'd just like to share my working response with NextJS v11.

I want dynamic routing at two levels. E.g.:

  • {siteroot}/dynamicPage
  • {siteroot}/dynamicUrlSection/dynamicPage

My folder structure is:

  • /pages/[section]/[page].tsx
  • /pages/[section]/index.tsx

This way, the "dynamicPage" path at the root is handled by index.tsx, and nested routes are handled by [page].tsx

BONUS INFO: I am working with Contentful as a CMS. I use a single content model for all the pages at both levels. The model has "section" and "page" properties. The entries that serve the root dynamic pages (i.e. /pages/[section]/index) have a compound value in the "page" property of {section}-index. I then have to be a bit smart in my client code:

if (!page) {
  page = `${section}-index`;
}

await fetchData(section, page);

1 Comment

Thank you. The docs don't seem to mention the ability to name folders with the[slug] for these kinds of routes.
4

You have 2 choices:

  1. Using v9 Dynamic Routing by calling the folder as [dynSlag] and putting your page file inside.
  2. Using custom server and routing, you will need to define a custom server, map your path to a specific next page.

2 Comments

Thanks I find the solution just create a folder named [ippage]
@felixmosh the 2nd link doesn't seem to work any longer
2

Using the example right from the NextJs documentation, I use this hack, maybe you could use it.

<Link href="/posts/[id]" as={`/posts/${subFolder}${id}`}>

"as" will have a value like /posts/nested_subfolder_file.md

And in the getPostData function, just do this little change:

  const nestedPaths = id.split('_')
  const fileName = `${nestedPaths.pop()}.md`
  const fullPath = path.join(postsDirectory, ...nestedPaths, fileName)

Comments

0

I managed to have something working for my use case (rest API): http://localhost:3000/api/articles/1/highlights/2

Folder structure:

- articles
  - index.ts
  
  - [article_id]
    - index.ts
    
    - highlights
      - index.ts
      - [highlight_id].ts 

[article_id]/index.ts file

import type { NextApiRequest, NextApiResponse } from "next";

const handler = async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
  const { article_id } = req.query
  return res.status(200).json(`article: ${article_id}`);
};

export default handler;

highlights/[highlight_id].ts file

import type { NextApiRequest, NextApiResponse } from "next";

const handler = async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
  const { highlight_id } = req.query
  return res.status(200).json(`highlight: ${highlight_id}`);
};

export default handler;

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.