Next.js Discord

Discord Forum

Optional Catch-all Segments and generateStaticParams

Answered
Russian Blue posted this in #help-forum
Open in Discord
Russian BlueOP
Given this route pages/shop/[[...slug]].js,
How to statically generate the root page (/shop in the example above?

I understand for non-optional catch-all segments
pages/shop/[...slug].js — we can statically generate segments like so:

export function generateStaticParams() {
return [{ slug: ["a"] }, { slug: [b] }];
}

// generates
// /shop/a
// /shop/b

I’m wondering if possible to do this for the ‘root’ page when using optional catch-all segments

Thanks for the help!
Answered by joulev
hmm try slug: [] or slug: ["index"]
View full answer

2 Replies

Answer
Russian BlueOP
got it!

an empty array does generate the the root page. in my 'shop' example above,

export function generateStaticParams() { return [{ slug: [] }, { slug: ["a"] }, { slug: ["b"] }]; }

// generates // /shop // /shop/a // /shop/b

confirmed this by running npm run build and inspecting the build folder: .next/server/app

found shop.html, a.html and b.html files

thanks for the help &1159364112724262982 !