Next.js Discord

Discord Forum

Why am I getting a 404 with dynamic params on a generated page?

Answered
Himalayan posted this in #help-forum
Open in Discord
Avatar
HimalayanOP
Why am I getting a 404 when visiting /posts/general/post-1

app/posts/[category]/[slug]/page.tsx
export const dynamicParams = false;

export async function generateStaticParams() {
  const { data } = await getClient().query({
    query: PostSlugsDocument,
  });
  const { items } = data?.postSlugs || {};

  if (!items) {
    return [];
  }

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


The page works when dynamicParams is true, but 404's when false.
Answered by Ray
which mean post-1 not get generated at build time
View full answer

18 Replies

Avatar
HimalayanOP
Could it be that the page is only ever rendering dynamically?
Avatar
which mean post-1 not get generated at build time
Answer
Avatar
@Himalayan Why am I getting a 404 when visiting `/posts/general/post-1` `app/posts/[category]/[slug]/page.tsx` export const dynamicParams = false; export async function generateStaticParams() { const { data } = await getClient().query({ query: PostSlugsDocument, }); const { items } = data?.postSlugs || {}; if (!items) { return []; } return items?.map(post => ({ category: post?.category?.slug, slug: post?.slug, })); } The page works when `dynamicParams` is `true`, but `404`'s when `false`.
Avatar
export async function generateStaticParams() {
  const { data } = await getClient().query({
    query: PostSlugsDocument,
  });
  const { items } = data?.postSlugs || {};

  console.log(items) <-- see if you get the items

  if (!items) {
    return [];
  }

  return items?.map(post => ({
    category: post?.category?.slug,
    slug: post?.slug,
  }));
}
Avatar
@Himalayan Yeah logs correctly
Avatar
could you show the log?
Avatar
@Ray could you show the log?
Avatar
HimalayanOP
Image
(post-1) is an example name by the way
Avatar
no post-1 here
and no general
Avatar
@Ray no post-1 here
Avatar
HimalayanOP
You may be on to something, will take a closer look thank you
Avatar
@Ray and no general
Avatar
HimalayanOP
As a matter of fact none of those posts or categories should exist, was all updated.
So perhaps a caching issue
Avatar
HimalayanOP
Yeah was cache, deleted .next and it's refreshed
Avatar
@Ray what is getClient()?
Avatar
HimalayanOP
export const { getClient } = registerApolloClient(() => {
  return new NextSSRApolloClient({
    cache: new NextSSRInMemoryCache(),
    link: new HttpLink({
      uri: `${CONTENTFUL_BASE_URL}${CONTENTFUL_SPACE_ID}`,
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${CONTENTFUL_DELIVERY_TOKEN}`,
      },
    }),
  });
});
Avatar
@Himalayan js export const { getClient } = registerApolloClient(() => { return new NextSSRApolloClient({ cache: new NextSSRInMemoryCache(), link: new HttpLink({ uri: `${CONTENTFUL_BASE_URL}${CONTENTFUL_SPACE_ID}`, headers: { "Content-Type": "application/json", Authorization: `Bearer ${CONTENTFUL_DELIVERY_TOKEN}`, }, }), }); });
Avatar
 const { data } = await getClient().query({
    query: PostSlugsDocument,
    context: {
      fetchOptions: {
         cache:'no-store'
      },
    },
  });

I think you can disable the data cache like this since you are building static page