Maaz Zindani

Loading the index…

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

React Router shows a blank page or the error screen for URLs that match no route

ErrorA mistyped URL shows a blank page or a default error screen

Short answer

React Router only renders what you register. Add a catch-all splat route with the path *, throw a 404 response from it, and export an ErrorBoundary that recognises the 404 and renders your not found page. Otherwise the default error screen appears.

  • Applies to React Router 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the registered routes. If there is no route with the path *, unknown URLs have nowhere to go.

    grep -rn "route(" app/routes.ts
  2. 2Match what you see, then jump to its fix

    1. 1Every unknown URL shows the same default errorNo catch-all route is registeredFix
    2. 2The status is 404 but the page shows the default error textThe 404 is thrown but nothing renders it nicelyFix

What does the error look like?

  • A mistyped URL shows a blank page or a default error screen
  • No custom 404 page appears for an unknown path

Cause 1 of 2

No catch-all route is registered

1Check

A splat route matches any URL that no other route matched.

cat app/routes.ts

2Fix

Register a route with the path * pointing at its own module, and throw a 404 response from its loader.

// app/routes.ts
import { type RouteConfig, route, index } from "@react-router/dev/routes";

export default [
  index("./home.tsx"),
  route("*", "./catchall.tsx"),
] satisfies RouteConfig;

// app/catchall.tsx
export function loader() {
  throw new Response("Page not found", { status: 404 });
}

3Verify

Open a URL that does not exist.

curl -I http://localhost:5173/does-not-exist

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 2

The 404 is thrown but nothing renders it nicely

1Check

A thrown response is shown by the closest error boundary. Without your own, the default one renders.

grep -rn "ErrorBoundary" app

2Fix

Export an ErrorBoundary from the catch-all module (or the root) and branch on the 404.

import { isRouteErrorResponse } from "react-router";

export function ErrorBoundary({ error }: { error: unknown }) {
  if (isRouteErrorResponse(error) && error.status === 404) {
    return <h1>Page not found</h1>;
  }
  return <h1>Something went wrong</h1>;
}

3Verify

Reload the unknown URL and read the page.

npm run dev

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Keep the catch-all route last in the list, as the fallback.
  • Use the same 404 handling for missing records inside loaders, with a thrown data response.
  • Check the status code too, so search engines do not index the not found page as a normal one.

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.