open-next icon indicating copy to clipboard operation
open-next copied to clipboard

Incorrect routing when using generateStaticParams

Open leanderiversen opened this issue 2 years ago • 13 comments

Hi,

We have had an issue relating to generateStaticParams, and have managed to narrow it down to being an OpenNext issue. The project consists of dynamic route segments and folder based routing, like this:

  • app/[locale]/[...slug]/page.tsx
  • app/en-gb/examples/example/page.tsx

The pages are fetched from our CMS and looped through to return the locale and slug for the dynamic route segments. To achieve the required URL structure, we have had to create empty pages within our CMS, which makes the folder based routing as seen above.

When developing locally with the production configuration, we can see that folder based routing is being served, as expected. Once deployed, the empty pages from the CMS is served instead, causing us to find workarounds to exclude these from generateStaticParams.

Happy to provide more information if required.

Versions Next: 14.0.4 (app router) SST: 2.38.4 OpenNext: 2.3.1

leanderiversen avatar Dec 11 '23 10:12 leanderiversen

Hey can you try 2.3.3? If that fails, can you try the v3 rc? If both fail, we need to properly handle i18n... last I checked nextjs has issues with that too.

khuezy avatar Dec 11 '23 14:12 khuezy

I have just tried 2.3.3, but it has not resolved the issue. Is it possible to target the RC using openNextVersion? If so, how? Thank you!

leanderiversen avatar Dec 11 '23 15:12 leanderiversen

You need to use a custom SST to test V3 https://open-next.js.org/v3.

From what i understand, this will likely not solve your issue. But i'm not sure i completely understand your issue though. Could you give an example of one of the empty return from generateStaticParams and what url you use to access this specific route.

conico974 avatar Dec 11 '23 15:12 conico974

Hi,

In the dynamic route, we have a generateStaticParams that does a fetch call to get all the pages from the CMS. This is then mapped through to populate the locale and slug segments:

export async function generateStaticParams() {
  const pages = await exampleCMSCall();

  return pages.map((page) => {
    // Logic to exclude duplicated pages

    return {
      locale: page.locale,
      slug: page.slug,
    };
  });
}

In the CMS, we have slugs like example/placeholder/test-page. The placeholder page is empty, but is required to create the page hierarchy in the CMS. We then have a static route for example/placeholder, which is supposed to render something custom. It is the static route that renders locally when visiting example/placeholder, which is what we expect. When deployed, it is the dynamic route (placeholder page) that renders, which is not what we want.

I hope that makes more sense.

leanderiversen avatar Dec 11 '23 15:12 leanderiversen

Ok i think i got it.

Just a couple more precision, when you say it renders locally, you're talking about next start not next dev right ?

Also when you talk about the example/placeholder static page, you mean you have a [locale]/example/placeholder/page.tsx page or just a example/placeholder/page.tsx ?

Last thing page.slug is an array right ?

conico974 avatar Dec 11 '23 16:12 conico974

Yes, it works with next start.

This is the two paths:

  • app/[locale]/[...slug]/page.tsx
  • app/en-gb/example/placeholder/page.tsx

For the first one of these, the route segments could be populated with "en-gb" and "example/placeholder", meaning that the URLs would be identical. With next start, it uses the static route, but not once deployed.

Finally, yes, page.slug is an array.

leanderiversen avatar Dec 11 '23 17:12 leanderiversen

I've tried to replicate this issue locally quickly, but it doesn't even work with next start unless i set my en-gb/example/page.tsx as SSR, and then it also works deployed. Could you provide a minimum repro I've created a [locale]/[...slug]/page.tsx

export function generateStaticParams() {
  return [
    {
      locale: 'en-gb',
      slug: ['example'],
    },
    {
      locale: 'fr-FR',
      slug: ['page'],
    },
    {
      locale: 'en-gb',
      slug: ['example', 'page'],
    }
  ]
}

export default function Page({ params: { slug }}: { params: { slug: string[] }}) {
  return (
    <div>
      <h1>Page</h1>
      <p>Dynamic</p>
      <p>{slug.join('/')}</p>
    </div>
  )
}

My en-gb/example/page.tsx

export const dynamic = "force-dynamic"

export default function Page() {
  return (
    <div>
      <h1>Page</h1>
      <p>Static</p>
    </div>
  )
}

conico974 avatar Dec 11 '23 18:12 conico974

We should add another e2e (or update app-router) to use localization to catch these issues.

khuezy avatar Dec 11 '23 19:12 khuezy

The repo can be found here: https://github.com/leanderiversen/open-next-331. The structure matches the real repo, including all layout.tsx and page.tsx, although simplified.

I have not had time to deploy this, but I can see that the /en-gb/first/second/ uses the static path when running next start, which is the expected result.

leanderiversen avatar Dec 11 '23 19:12 leanderiversen

I found the issue it's a next one, you should create an issue in next repo.

I cloned your repo run next build then next start and it worked as you've tested, but we need output: "standalone" (we set this automatically via an env variable), as soon as i've rebuild and run either next start or node .next/standalone/server.js i get the dynamic route.

I let the issue open in case someone has the same issue, if you could update the issue with the issue you open in next repo that would be great

conico974 avatar Dec 11 '23 20:12 conico974

Thank you very much for investigating!

leanderiversen avatar Dec 11 '23 20:12 leanderiversen

The issue has been reported in the Next repository, and can be found here: https://github.com/vercel/next.js/issues/59505

Feel free to close this issue.

leanderiversen avatar Dec 11 '23 20:12 leanderiversen

I will be closing this issue, as it appears to be fixed in one of the latest Next.js canaries.

leanderiversen avatar Jan 05 '24 08:01 leanderiversen