Hey everyone I'm struggling to create dynamic routes in Next.js where the middle part is optional.
I have a single custom type which is a distribution page. And I have repeatable custom types which are product pages. This product pages can have categories but they are optional. The category field on the product page is a content relationship field to another custom type which resembles the category.
Unfortunately I can't create this URL structure. I tried it with getStaticPaths from Next.js and the optional catch all routes and the route resolver from prismic.
What I want in the end is:
- distribution page: /distribution
- product page: /distribution/product
or product page with category when the user added this field in prismic: /distribution/category/product
My folder structure looks like this:
/pages
/distribution
/[uid].tsx
And my route resolver:
export const routeResolver = {
routes: [
{
type: 'index',
path: '/',
},
{
type: 'distribution',
path: '/distribution',
},
{
type: 'product-page',
path: '/distribution/:category?/:uid',
resolvers: {
category: 'category',
},
},
],
}
And here is my getStaticPaths
function:
export async function getStaticPaths() {
const docs = await Client().query(
Prismic.Predicates.at('document.type', 'product-page'),
{ lang: '*' }
)
return {
paths: docs.results.map((doc: any) => {
return {
params: {
uid: doc.uid,
category: doc.data.category.uid,
},
}
}),
fallback: false,
}
}
The routing for /distribution and /distribution/:uid works but /distribution/:category/:uid throws a 404.
Thanks in advance for every help, I'm sure this was solved before and I'm just overseeing a small detail!