next.js: Next.js default not-found/404 page doesn't render on Vercel deployments

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/dry-dust-r83nds?file=%2Fnext.config.js%3A10%2C32

To Reproduce

  1. Start the application locally in dev.
  2. Navigate to a page that doesn’t exist, i.e. http://localhost:3000/nosuchpage and see Next’s default 404 page.
  3. Host the Next.js app on Vercel.
  4. Navigate to a page that doesn’t exist, i.e. http://helloworld.com/nosuchpage and we see a broken version of the homepage rather than Next’s not-found/404 page.

Current vs. Expected behavior

When navigating to incorrect/non-existent paths, the Next.js default not-found/404 page doesn’t render on Vercel deployments, but does during local development.

We would expect to see the 404/not-found page in the Vercel’s production deployment, just as we see it in dev.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030
Binaries:
  Node: 21.3.0
  npm: 10.2.4
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.5-canary.46
  eslint-config-next: 14.0.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure, App Router, Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

We are hosting our Next.js app on Vercel.

As per these Next.js docs we have also tried making our own not-found.tsx component and placed it under ./app so all incorrect paths should result in the custom 404 being displayed, but they don’t on Vercel, but do during local development. As suggested in other threads, we have also tried placing it throughout the application, but with no with success.

Note: running in production mode locally still gives a 404

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 13
  • Comments: 26

Commits related to this issue

Most upvoted comments

Any fix to this solution, having the same issue. I have not-found.tsx file in the app directory, works when run locally, but it never gets rendered in production for incorrect url?

this worked for me

inside app create a file at […not_found]/page.tsx

import {notFound} from "next/navigation"

export default function NotFoundCatchAll() {
  notFound()
}

Reason: Since dynamic routes are resolved after static routes, we can solve it by creating a dynamic catch-all route using a […not_found] folder and add it to our app folder.

this worked for me

inside app create a file at […not_found]/page.tsx

import {notFound} from "next/navigation"

export default function NotFoundCatchAll() {
  notFound()
}

Reason: Since dynamic routes are resolved after static routes, we can solve it by creating a dynamic catch-all route using a […not_found] folder and add it to our app folder.

this works, though sucks that this is needed 😢

Project Settings / Change Framework preset from Other to Next.js Redeploy the project.

Don’t know if this will work in Next but I was getting the same issue with Vite. I created a vercel.json file in the root directory and added the following.

{
  "rewrites": [{ "source": "/(.*)", "destination": "/" }]
}

I have this set-up with react-router-dom in App.jsx.

<Router>
  <Navbar />
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  <Footer />
</Router>

Hope this helps someone.