Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateStaticParams not Passing Parent Params to Child in Nested Dynamic Routes #53717

Open
1 task done
nikolailehbrink opened this issue Aug 8, 2023 · 6 comments · May be fixed by #55912
Open
1 task done

generateStaticParams not Passing Parent Params to Child in Nested Dynamic Routes #53717

nikolailehbrink opened this issue Aug 8, 2023 · 6 comments · May be fixed by #55912
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@nikolailehbrink
Copy link

nikolailehbrink commented Aug 8, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:08:47 PST 2022; root:xnu-8792.61.2~4/RELEASE_X86_64
    Binaries:
      Node: 18.12.1
      npm: 9.7.1
      Yarn: 1.22.19
      pnpm: 8.6.6
    Relevant Packages:
      next: 13.4.14-canary.0
      eslint-config-next: 13.4.13
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router

Link to the code that reproduces this issue or a replay of the bug

https://github.com/nikolailehbrink/generateStaticParams-Bug

To Reproduce

  • Clone the GitHub repository that contains the bug.
  • Install the necessary dependencies with npm install or yarn install.
  • Start the development server with npm run dev or yarn dev.
  • Navigate to a nested route such as http://localhost:3000/12314/iasd in your browser.
  • Observe that the child page is accessible and the slug parameter is not available in the child's generateStaticParams.

Describe the Bug

I encountered a bug when using the generateStaticParams function with nested dynamic routes in Next.js. The parent's parameters are not being passed to the child's generateStaticParams, causing unexpected behavior in the generated static pages. According to the your documentation, the expected behavior is to "Generate the parent segments first and use the result to generate the child segments." However, in the provided example, the behavior is not consistent with this documentation, and the slug parameter from the parent route does not get passed to the child.

Expected Behavior

Expected Behavior:
The slug parameter generated in the parent's generateStaticParams should be available in the child's generateStaticParams.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1509

@nikolailehbrink nikolailehbrink added the bug Issue was opened via the bug report template. label Aug 8, 2023
@yuki-yamamura
Copy link

This problem also happened in my project which has the following environment.

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:52 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.15.0
      npm: 9.5.0
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.13
      eslint-config-next: 13.4.12
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
    Next.js Config:
      output: N/A

What I want to do is like this:

// app/tags/[tag]/page.tsx
export const generateStaticParams = async (): Promise<{ tag: string }[]> => {
  const tags = await getTags();  // ['tagA', 'tagB', 'tagC']
  return tags.map((tag) => ({ tag }));
};


// app/tags/[tag]/page/[pageNumber]/page.tsx
export const generateStaticParams = async ({
  params: { tag },
}: {
  params: { tag: string };
}): Promise<{ pageNumber: string }[]> => {
  // Since tag is always undefined, getting articles by tag is to fail.
  const articles = await getArticlesByTag(tag);
  // ...
  }));
};

@leerob leerob added the linear: next Confirmed issue that is tracked by the Next.js team. label Aug 11, 2023
@rustairov
Copy link

rustairov commented Aug 14, 2023

Same issue.

I've also tried to add generateStaticParams in layout.tsx and it didn't pass static params into child pages/layouts.

@oliverkidd
Copy link

Can we get any updates on a temporary workaround on this issue? I'm having the same issue

@viktorronnback
Copy link
Contributor

I found a workaround, which is to use generateStaticParams in the layout-file instead of page in the parent folder (template file does not work either). See this repo for an example: https://github.com/viktorronnback/generate-static-params-bug-53717

I'll keep looking into why this bug happens and try to make a PR. I assume it is a bug as, to my knowledge, it isn't stated in the docs that using layout in the parent is a requirement to pass params to child generateStaticParams functions. And even if it was a requirement, I think there should be some sort of build warning about using it.

@yuki-yamamura
Copy link

yuki-yamamura commented Sep 19, 2023

@viktorronnback
Thanks. Using a layout in the parent folder works fine.

As far as I've been trying, the bottom up pattern has no problem.

@Antonio-Laguna
Copy link

Anyone that might be pulling their hair. the workaround introduces issues with @slots and intercepting routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants