Jegs
Jegs

Reputation: 689

How to access params in getStaticPaths?

I have a Next.js app and the CMS is Contentful.

File structure relevant for this question is as follows:

pages
-[category]
  -[slug].js

What I want to do is access the value of category when a users visits category/slug.

Right now I am hardcoding category. Is there any way I can access category using params?

const category = 'mains';

export async function getStaticPaths() {
    let data = await client.getEntries({ content_type: category });
    return {
        paths: data.items.map((path) => ({
            params: { category: category, slug: path.fields.slug }
        })),
        fallback: false
    };
}

Upvotes: 0

Views: 402

Answers (1)

Ivan V.
Ivan V.

Reputation: 8101

getStaticPaths are used for prerendering paths i.e before the app is deployed. You need to use getStaticProps for rendering on-demand, there, you will have a context.params object with all the params (ctx.params.category, ctx.params.slug)

Upvotes: 1

Related Questions