Replies: 2 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Hi @mildfuzz 👋 You’re right — the basePath isn’t exposed directly on the NextRequest object by design. It’s handled internally by the Next.js router layer and stripped out before the request reaches middleware or API routes. However, there are a couple of reliable ways to work around this depending on your setup: ✅ Option 1: Use next.config.js and Import It Since the basePath is static and known at build time, you can import it where you need it: // next.config.js // middleware.js export function middleware(req) { ✅ Works well if you’re building each app independently. 🧠 Option 2: Derive it Dynamically from the URL If you’re running multiple Next.js apps under different base paths (e.g. /app1, /app2, etc.), you can infer it from req.nextUrl.pathname: const { pathname, origin } = req.nextUrl; return NextResponse.redirect(new URL( This lets your redirect logic automatically adapt to whichever app the request came from — without hardcoding each path. ⚙️ Why It’s Not Included Next.js removes the basePath before building the request context to ensure all internal routing stays consistent. 💬 Summary basePath is not exposed intentionally. You can import it from next.config.js or detect it dynamically from the URL. This allows middleware-based redirects to work across multi-app environments safely. Hope this helps you keep your redirect logic DRY and dynamic! 🚀 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I have a multi-application stack, with several applications running on custom basePath's
I want to write a single function that will redirect a user from one to the other, but at the moment I have having to hard code the route back because I can't deduce the
basePath
from the request.I have logged out all the values in
request.nextUrl
and it doesn't appear to be in there, is this by design?Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions