The React Router package
For: a developer of a React Router 8 app with server middleware.
What it does: this page lists the exports of the package, the steps of the mount, and the paths that the protect rule gets. The export check holds this page to packages/react-router/src/index.ts.
Before you start
Section titled “Before you start”- Read The API reference for the auth object that the package takes.
- Node 22.22 or newer. React Router 8.4 needs it (its engines field, read 2026-10-15). The binding adds no floor of its own.
- Mount core:
app/routes/auth.$.tsholdsexport const { loader, action } = authRouteModule(getAuth);. - Read the session:
app/root.tsxholdsexport const middleware = [authMiddleware({ auth: getAuth })];. - Read the session in a loader:
getSession(context). - Protect paths:
authMiddleware({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }). - On Workers, pass the hook of the runtime:
authRouteModule(getAuth, { waitUntil }), withimport { waitUntil } from "cloudflare:workers". The option also gets the loader arguments, so(p, { context }) => context.get(cloudflare).ctx.waitUntil(p)works with your own context key.
Exports
Section titled “Exports”| Export | What it does |
|---|---|
authRouteModule |
Answers the loader and the action of the resource route of core. |
authMiddleware |
Answers the middleware that sets the session. It sends a person with no session on a protected path to the sign-in page, and adds the renewed cookie. |
getSession |
Answers the session that the middleware set, or null. |
sessionContext |
The router context of the session. |
AuthSource |
The auth instance, or a function that makes it at the first request. |
RouteModuleOptions |
The options of authRouteModule: waitUntil, the hook that keeps work alive after the answer. It has no default. |
RouterAuthOptions |
The options of authMiddleware: auth, protect, signInPath, and basePath. |
The paths that protect gets
Section titled “The paths that protect gets”protectgets the path as React Router matched it. “.data” is removed, and each segment is decoded once.- A decoded “/” stays
%2F, and “.” and “..” are not resolved. So “/%61ccount” reaches the rule as “/account”. - Write a rule in decoded text: “/café”, “/σ”.
- React Router matches a path without case by default, so “/ACCOUNT” runs the page of “/account”.
- React Router ignores trailing slashes on every route that is not a splat, so “/account/” renders “/account”.
- So the rule gets more than one form. These are the path as matched, its lower-case form, and both forms with trailing slashes removed.
- A fifth form is the matched path in the case of the route’s own text. For example, “/myaccount” on the route “myAccount” gives “/myAccount”.
- A path needs a session when the rule holds for any form. Write a rule in the route’s own text.
- Still set
caseSensitive: trueon each protected route, so “/ACCOUNT” gets a 404. - A parameter value or a splat value keeps the case of the request, because the route has no text for it.
- So a rule on “/en/myAccount” for “:lang/myAccount” misses “/EN/MYACCOUNT”. Compare such segments in lower case in the rule.
- A rule must hold for every path that the route matches. Use a prefix rule for a splat or a parameter.
- An exact rule covers one route only. Name each form of an optional segment: “:lang?/account” renders for “/en/account”, which a rule on “/account” does not name.
- A data request “/account/…data” reaches a splat route “account/*” as “/account/..”, and an exact rule misses it.
nextkeeps the page URL in URL form, with the case of the request.
How it behaves
Section titled “How it behaves”- React Router 8 gives a loader a context with the keys of the app only. So the binding cannot read the ExecutionContext of Workers. With no
waitUntiloption, the work after the answer runs detached. On Workers core then waits for the work before it answers (ruling 29 of plan 12i). - The package takes
react-router8.4.0 or a later 8 release as a peer dependency. - The middleware reads no session for a route of core. Core reads and renews the session itself.
- The middleware skips the session read only when the raw path and the matched path are both under the base path. Else it judges the request as a page.
- Core matches its routes on the raw path. A path that the router decodes to a path of core, such as “/%61uth/x”, never runs a page.
- When the rule holds for the decoded path and there is no session, the answer is the sign-in redirect. Else core answers 404.
- After the sliding refresh, the page’s answer carries the renewed session cookie.
- A function given to both
authRouteModuleandauthMiddlewareruns twice for a request to core. Make it return one instance per environment. - React Router runs the loader of a resource route for an OPTIONS request. So core answers a cross-origin preflight to its routes.
- The sign-in redirect goes to the origin of
request.url, as the React Router server builds it. The binding never reads the host of an absolute-form request target.@react-router/nodebuilds the URL increateRequestof@remix-run/node-fetch-server0.14.2 (read 2026-10-15). It takes thehostoption, then the forwarded host undertrustProxy, then:authority, then the Host header, then localhost.@react-router/express8.4.0 takesreq.protocolandreq.hostname. Each adapter adds the request target after that host, so an absolute-form target cannot set the origin. Set thehostoption, or trust a forwarded host only behind your own proxy.