Next.js Discord

Discord Forum

dynamicParams from the bottom up - not working

Unanswered
Himalayan posted this in #help-forum
Open in Discord
HimalayanOP
In this structure app/posts/[category]/[slug]/page.tsx
page.tsx is generating params for both [category] and [slug].
And declaring dynamicParams as false
Like so:

export const dynamicParams = false;

export async function generateStaticParams() {
  const { data } = await getClient().query({
    query: PostSlugsDocument,
    context: {
      fetchOptions: {
        cache: "no-store",
      },
    },
  });
  const { items } = data?.postSlugs || {};

  if (!items) {
    return [];
  }

  return items?.map(post => ({
    category: post?.category?.slug,
    slug: post?.slug,
  }));
}


This works fine for [slug], visiting a page not generated returns a 404.
However in [category], visiting a page not generated returns a contentless page.
Even if I declare export const dynamicParams = false; in [category] as well.
How do I force category to return a 404 if the page wasn't generated in generateStaticParams in [slug]

28 Replies

HimalayanOP
here is what has been generated
@Himalayan here is what has been generated
no html file?
HimalayanOP
@Ray indeed not
which mean the page is not generated at build time
@Himalayan <@743561772069421169> indeed not
check the paths here
const paths = items?.map(post => ({
    category: post?.category?.slug,
    slug: post?.slug,
  }));
console.log(paths)
return paths
@Ray check the paths here ts const paths = items?.map(post => ({ category: post?.category?.slug, slug: post?.slug, })); console.log(paths) return paths
HimalayanOP
[app]  ✓ Compiled /posts in 468ms (3037 modules)
[app] [ { category: 'general', slug: 'welcome-to-neilmorgan-dev' } ]
So the generated page renders fine, but I shouldn't be able to render a random page - that should 404
in [category]
And yeah running build it looks like it doesnt generate
Route (app)                                     Size     First Load JS
┌ ○ /                                           139 B          84.3 kB
├ ○ /_not-found                                 874 B            85 kB
├ λ /api/disable-draft                          0 B                0 B
├ λ /api/draft                                  0 B                0 B
├ ○ /posts                                      982 B           465 kB
├ λ /posts/[category]                           982 B           465 kB
└ ● /posts/[category]/[slug]                    982 B           465 kB
    â”” /posts/general/welcome-to-neilmorgan-dev
+ First Load JS shared by all                   84.1 kB
  ├ chunks/472-70268598ad42ecc9.js              28.9 kB
  ├ chunks/fd9d1056-5f9fa447a5be7208.js         53.3 kB
  ├ chunks/main-app-22eaf81abaf0ba05.js         223 B
  â”” chunks/webpack-1cac2626960613e9.js          1.75 kB
@Himalayan in [category]
you get a random page on /posts/111/welcome-to-neilmorgan-dev?
I think here is the problem?
λ /posts/[category]
@Ray you get a random page on `/posts/111/welcome-to-neilmorgan-dev`?
HimalayanOP
This throws
Error: Cannot find module './vendor-chunks/@apollo.js'
@Himalayan This throws Error: Cannot find module './vendor-chunks/@apollo.js'
oh so what url are you visiting and you got random page?
@Ray you get a random page on `/posts/111/welcome-to-neilmorgan-dev`?
HimalayanOP
Sorry this is a 404, restarted dev
@Himalayan Sorry this is a 404, restarted dev
ok so the problem solved?
HimalayanOP
This - http://localhost:3000/posts/foo
Should 404, but it doesnt, it returns a contentless page
@Ray I think here is the problem? ` λ /posts/[category] `
yes because this page is dynamic
could you show a screenshot of your folder stucture?
HimalayanOP
app/posts/[category]/[slug]/page.tsx - this is not dynamic as I am using dynamicParams = false
The parent should also not be dynamic
/posts/[category] is
HimalayanOP
Sorry not seeing the issue
HimalayanOP
Will take a closer look when back to the laptop this evening
HimalayanOP
Ah, so the child isn't succesfully generating the parent?
@Himalayan Ah, so the child isn't succesfully generating the parent?
I think you need to generate /posts/[category] because it is a different route