Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A dynamic route like /posts/[id] returns 404 in the Next.js Pages Router

Error404 | This page could not be found

Short 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.

  • Applies to Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 build
  2. 2Match what you see, then jump to its fix

    1. 1Old items load, new ones 404 until the next buildfallback: false and the path was not generated at build timeFix
    2. 2The path is in your data but still 404sThe params keys or values do not match the routeFix
    3. 3The site uses output: 'export' and unknown paths 404 on the hostStatic export cannot use fallbackFix
    4. 4/blog/a works, but /blog or /blog/a/b gives 404The URL has more or fewer segments than the file name allowsFix

What does the error look like?

  • 404 | This page could not be found
  • A [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\].js

2Fix

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/3

Confirmed by nextjs.org

Did this fix it?

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/posts

2Fix

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/1

Confirmed by nextjs.org

Did this fix it?

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 build

Confirmed by nextjs.org

Did this fix it?

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/b

3Verify

Request the URLs that failed.

curl -I http://localhost:3000/shop

Confirmed by nextjs.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.