Show a 404 page for unknown URLs in React Router
Blank page on a URL that no route matchesShort answer
React Router only renders a page when a route matches the URL. To show a 404 page for everything else, add a route whose path is a bare star, as the last sibling. In framework mode, register route("*", file) and throw a 404 Response from its loader so the status code is real as well.
Fix it fast
1Find out which cause you have
Find out which routing mode the app uses. The file that defines your routes tells you, and each mode adds its catch-all in a different place.
grep -rn -E "createBrowserRouter|<Routes|BrowserRouter|routes.ts" src app --include=*.tsx --include=*.jsx --include=*.ts --include=*.js2Match what you see, then jump to its fix
What does the error look like?
Blank page on a URL that no route matchesAn unknown URL shows the router default error screen instead of your own page
Cause 1 of 2
No catch-all route exists in a JSX or object route tree
1Check
Open the file with your Routes or createBrowserRouter list. A route path that ends with /* matches any text after the slash, including more slashes (the docs call this a splat). A bare star is that rule with nothing in front of it.
grep -rn 'path=' src | head -302Fix
Add one route with a bare star as its path and give it a NotFound component. Put it in the same list as the other top-level routes. Specific paths such as about still win over the star.
import { BrowserRouter, Routes, Route } from "react-router";
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>3Verify
Start the dev server and open a URL that does not exist, such as /no-such-page. The NotFound component renders and /about still shows About.
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:5173/aboutConfirmed by reactrouter.com
Cause 2 of 2
Framework mode: routes.ts has no star route
1Check
Open app/routes.ts. In framework mode the catch-all is declared there with the pattern *, pointing at a route module file.
cat app/routes.ts2Fix
Add a route with the pattern * as the last entry, then create that file. Throw a Response with status 404 from its loader, so browsers and crawlers get a real 404 status and not just a page that looks like one.
// app/routes.ts
route("*", "./catchall.tsx"),
// app/catchall.tsx
export function loader() {
throw new Response("Page not found", { status: 404 });
}3Verify
Request an unknown URL and read the status line of the response.
curl -I http://localhost:5173/no-such-pageConfirmed by reactrouter.com
How do I stop it coming back?
- Keep the star route as the only route with that path, so it is easy to find.
- In framework mode, return a 404 status from the loader, so search engines do not index the not-found page as a normal page.
- Add an unknown URL to your smoke test or end-to-end test, next to the happy-path routes.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix