Skip to content

Catch-all routes and SSG #10173

@Timer

Description

@Timer

@dpfavand in that case you'll want to use a catch-all route: https://nextjs.org/blog/next-9-2#catch-all-dynamic-routes

Potentially we can warn when you try to return a path including slashes, but the behavior is correct when using [slug].js, in your case you want [...slug].js.

@timneutkens thanks for the follow-up. I've tried two methods without success. Basically when specifying the slug value as a string in getStaticPaths, it isn't passed through to getStaticProps at all. When returning the slug value as an array, the build fails as the value must be a string.

Case 1, Assuming a file pages/[...slug].jsx, slug as string:

export async function unstable_getStaticPaths() {
	return [{ params: { slug: 'en/about' } }];
}

export async function unstable_getStaticProps({ params }) {
	console.log('params', params);
	return { slug: params.slug };
}

In the above case, params in getStaticProps is an empty object - no slug key.

Case 2, pages/[...slug].jsx, slug as array,

export async function unstable_getStaticPaths() {
	const allPaths = Object.keys(pathMap).map(slug => ({ params: { slug } }));
	return [{ params: { slug: ['en', 'about'] } }];
}
export async function unstable_getStaticProps({ params }) {
	console.log('params', params);
	return { slug: params.slug };
}

In case 2, the build fails with

> Build error occurred
{ Error: A required parameter (slug) was not provided as a string.
    at _validParamKeys.forEach.validParamKey (/project/node_modules/next/dist/build/utils.js:21:569)
    at Array.forEach (<anonymous>)
    at toPrerender.forEach.entry (/project/node_modules/next/dist/build/utils.js:21:495)
    at Array.forEach (<anonymous>)
    at Object.isPageStatic (/project/node_modules/next/dist/build/utils.js:17:122)
    at process._tickCallback (internal/process/next_tick.js:68:7) type: 'Error' }

Originally posted by @dpfavand in #9524 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions