Inconsistent basename behavior between Components and Middleware in Framework Mode
#14851
Replies: 3 comments
|
@KimotoYanke Your observation is reasonable: basename-aware URL generation in components and explicit middleware redirects are not always symmetrical by default. A practical pattern is to centralize redirect URL creation (for example a helper that prefixes So yes, your workaround style is generally the right defensive approach until middleware basename behavior is unified. Sources: |
|
This is intended behavior, and it comes from
Given that, your // app/lib/redirect-with-basename.ts
import { redirect } from "react-router";
export function redirectTo(path: string, init?: number | ResponseInit) {
return redirect(`${import.meta.env.BASE_URL}${path}`, init);
}and using |
|
This is by design rather than a bug. Client-side navigation ( So redirect targets have to include the base already. Your workaround is the idiomatic fix. Centralizing it in a small helper keeps it consistent: const BASENAME = "/your-base"; // the same value you pass to the router
const withBase = (path: string) => `${BASENAME}${path}`;
throw redirect(withBase("/login"));If you set basename from an env var, reuse that same variable here so the two never drift. Whether redirects should auto-apply basename is a maintainer call, but the current behavior is intentional. |
Uh oh!
There was an error while loading. Please reload this page.
Hi.
My team is developing a project using Framework mode with a
basenameconfigured in the URL. We're running into an issue wherebasenameis handled inconsistently depending on context.Reproduction repository: https://github.com/KimotoYanke/react-router-basename-test (basename is set to
/base/)Current Behavior
In components, basename is handled automatically and transparently. For example, a
<Link>ornavigate()call targeting the login page can be written simply as/login, and React Router correctly resolves it to/base/login.However, in middleware,
throw redirect("/login")does not apply the basename — it redirects to/logininstead of/base/login, resulting in a not-found error. As a workaround, we have to writethrow redirect(joinURL(import.meta.env.BASE_URL, "login"))Questions
basenameto redirect paths?basenamehandling?I'd appreciate any clarification. Thank you!
All reactions