A dynamic route like /posts/[id] returns 404 in the Next.js Pages Router
404 | This page could not be foundShort answer
With getStaticPaths and fallback: false, every path that is not returned at build time is a 404. Either return the path, or switch to fallback: 'blocking' or true. Also check that the params keys equal the folder name and that the values match the URL, because they are case-sensitive.
Fix it fast
1Find out which cause you have
List the paths the build generated. A path that is missing from the output is a path getStaticPaths did not return.
npx next build2Match what you see, then jump to its fix
- 1
Old items load, new ones 404 until the next buildfallback: false and the path was not generated at build timeFix - 2
The path is in your data but still 404sThe params keys or values do not match the routeFix - 3
The site uses output: 'export' and unknown paths 404 on the hostStatic export cannot use fallbackFix - 4
/blog/a works, but /blog or /blog/a/b gives 404The URL has more or fewer segments than the file name allowsFix
- 1
What does the error look like?
404 | This page could not be foundA [id] page works for some URLs and returns 404 for newer ones
Cause 1 of 4
fallback: false and the path was not generated at build time
1Check
The docs say that with fallback false any path not returned by getStaticPaths gives a 404, and new paths need another next build.
grep -n "fallback" pages/posts/\[id\].js2Fix
Use fallback: 'blocking' so unknown paths are rendered on the first request and then cached. Add revalidate in getStaticProps if pages must update.
export async function getStaticPaths() {
return { paths: [{ params: { id: '1' } }], fallback: 'blocking' }
}3Verify
Open a path that was not in the list.
curl -I http://localhost:3000/posts/3Confirmed by nextjs.org
Cause 2 of 4
The params keys or values do not match the route
1Check
The params object must use the same names as the file name, so pages/posts/[postId]/[commentId] needs postId and commentId. Values are case-sensitive: WoRLD matches only /WoRLD. A catch-all [...slug] needs slug as an array.
ls pages/posts2Fix
Rename the keys to match the folder, and normalize the casing of the values.
paths: posts.map((p) => ({ params: { id: String(p.id) } }))3Verify
Rebuild and open the exact path.
npx next build && curl -I http://localhost:3000/posts/1Confirmed by nextjs.org
Cause 3 of 4
Static export cannot use fallback
1Check
The docs state fallback: true and fallback: 'blocking' are not supported with output: 'export'.
grep -n "output" next.config.*2Fix
List every path in getStaticPaths and rebuild when data changes, or stop using static export so a server can render new paths.
// next.config.js: remove this line to allow fallback
// output: 'export',3Verify
Open the new path after a rebuild.
npx next buildConfirmed by nextjs.org
Cause 4 of 4
The URL has more or fewer segments than the file name allows
1Check
A plain [slug] file matches exactly one segment. A catch-all [...slug] matches one or more. Only the optional form [[...slug]] also matches the bare folder path.
find pages -name "*\[*"2Fix
Rename the file to the form that matches the URLs you use.
pages/shop/[[...slug]].js # matches /shop, /shop/a, /shop/a/b3Verify
Request the URLs that failed.
curl -I http://localhost:3000/shopConfirmed by nextjs.org
How do I stop it coming back?
- Return every public id from getStaticPaths, or use fallback: 'blocking' for large or changing data.
- Keep param names equal to the file name in one shared constant.
- Normalize slugs to lowercase when you create them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix