Maaz Zindani

Loading the index…

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

React Router renders a blank page because the URL does not start with the basename

Your error

<Router basename="/app"> is not able to match the URL "/" because it does not start with the basename, so the <Router> won't render anything.1 more line it can show

Fix it fast

Most common cause, 1 of 3

HashRouter was given the folder the app is served from

Drop the basename from HashRouter.

Fix
import { HashRouter, Routes, Route } from "react-router";

<HashRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/orders" element={<Orders />} />
  </Routes>
</HashRouter>
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The warning <Router basename="/app"> is not able to match the URL ... so the <Router> won't render anything means the path the router reads does not begin with its basename. HashRouter reads that path from the part after #, so a basename meant for the server folder never matches. Remove it there, or align BrowserRouter's basename and slash with the real path. Applies to React Router 6-8.

  • Applies to React Router 8.5, React Router 6.28 and 7.x (same warning), Vite 8.3, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Copy the URL inside the warning and compare it with where the browser really is. If it equals the part after # while the folder sits before #, that is cause 1. If it equals location.pathname and lacks the basename, cause 2. If it is the basename without its last slash, cause 3.

Run this first
console.log({ pathname: location.pathname, hash: location.hash })

Then find the line you see in this list and open its fix:

  1. 1HashRouter was given the folder the app is served fromThe browser shows /app/#/ or /app/#/orders, and the warning reports the URL as / or /orders
  2. 2BrowserRouter's basename differs from the path in the address barThe warning's URL is the same as location.pathname, for example / on the dev server while basename is /app
  3. 3The basename ends with a slash that the URL lacksbasename is /app/ and the browser is on /app, so the warning reports "/app"

What does the error look like?

  • <Router basename="/app"> is not able to match the URL "/" because it does not start with the basename, so the <Router> won't render anything.
  • HashRouter basename not working, the page stays blank

Cause 1 of 3

HashRouter was given the folder the app is served from

You seeThe browser shows /app/#/ or /app/#/orders, and the warning reports the URL as / or /orders

Check

A basename on HashRouter causes this warning whenever the basename names the server folder. HashRouter stores its route after the # sign, a piece of the address that never reaches the server, and React Router parses its pathname out of window.location.hash. The /app folder in front of # is not part of that path, so /orders never starts with /app.

Check
grep -rn "<HashRouter" src/

Fix

Drop the basename from HashRouter. The routes work from whatever folder serves index.html, because only the hash is matched.

Fix
import { HashRouter, Routes, Route } from "react-router";

<HashRouter>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/orders" element={<Orders />} />
  </Routes>
</HashRouter>

Verify

Open https://example.com/app/#/orders: the Orders page renders and the console has no basename warning.

Verify
console.log(document.querySelector('#root').childElementCount > 0)

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

BrowserRouter's basename differs from the path in the address bar

You seeThe warning's URL is the same as location.pathname, for example / on the dev server while basename is /app

Check

With BrowserRouter the warning means the real path does not begin with the basename. It shows up when basename is hard-coded to the production folder while the dev server serves the app at /, or after the app moves to another folder. The router then returns null, so nothing below it renders.

Check
grep -rn "basename=\|base:" src/ vite.config.*

Fix

Set the folder once, in Vite's base option, and pass the same value to the router through import.meta.env.BASE_URL. Strip the trailing slash that BASE_URL carries, for the reason in cause 3.

Fix
// vite.config.js
export default defineConfig({ base: '/app/' });

// main.jsx
import { BrowserRouter } from "react-router";

<BrowserRouter basename={import.meta.env.BASE_URL.replace(/\/$/, '')}>
  <App />
</BrowserRouter>

Verify

npm run dev opens the app under /app/, and a production build served from /app/ renders the same routes.

Verify
npm run build && npx vite preview

Confirmed by vite.dev

Did this fix it?

Cause 3 of 3

The basename ends with a slash that the URL lacks

You seebasename is /app/ and the browser is on /app, so the warning reports "/app"

Check

React Router keeps a trailing slash on basename so you can control trailing-slash rules, and its stripBasename check then requires the path to start with /app/ including that slash. A visit to /app has no slash after the folder name, so the match fails. Letter case is not the problem here: both sides are lowercased before the comparison.

Check
grep -rn "basename=" src/

Fix

Write the basename without a trailing slash. /app then matches /app, /app/ and /app/orders.

Fix
<BrowserRouter basename="/app">
  <App />
</BrowserRouter>

Verify

Load /app and /app/ in turn: both render the home route without the warning.

Verify
console.log(location.pathname)

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Derive the router basename from the bundler's base setting instead of typing the folder in two places.
  • Pick one router per deployment: HashRouter for hosts that cannot rewrite unknown paths, BrowserRouter with a basename when they can.
  • Open the deployed build once under its real folder before release, since the dev server at / hides a wrong basename.

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 on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

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