vercel.json redirects and Next.js middleware path conflicts
0 reputation · 20 Jun 2025, 01:43 UTC
0 reputation · 20 Jun 2025, 01:43 UTC
Vercel allows routing configuration via the vercel.json file using redirects and rewrites. Simultaneously, Next.js applications often implement middleware.ts to handle dynamic request routing, authentication, or localization.
A configuration that functions as expected during local development with vercel dev may behave differently in production. Specifically, when a path pattern is defined in both the vercel.json configuration and the Next.js middleware, the execution order and priority of these two layers can lead to unexpected 308 permanent redirects or 404 errors that do not manifest in the local emulation environment.
What is the definitive precedence order between vercel.json redirects and Next.js middleware in a production runtime? Does the Vercel edge network evaluate the static configuration before the middleware function is invoked?
In a production build on Vercel, the edge network applies the vercel.json redirects (and rewrites) before invoking any middleware.ts code. If a request matches a redirect rule, Vercel immediately sends the redirect response (typically a 308) and skips the middleware entirely. This is the opposite of the local vercel dev environment, where middleware runs first and redirects are evaluated afterward.
vercel.json containing:
{
"redirects": [
{
"source": "/old-path",
"destination": "/new-path",
"permanent": true
}
]
}
middleware.ts that logs a message and returns a simple response for /old-path.
import { NextResponse } from 'next/server';
export function middleware(request) {
console.log('middleware hit');
return NextResponse.next();
}
/old-path from a browser or curl -I https://your-app.vercel.app/old-path.
308 redirect to /new-path./only-middleware) and verify the middleware runs and returns the expected response.
{
"rewrites": [
{
"source": "/old-path",
"destination": "/new-path"
}
]
}
Do you also use rewrites or headers in your vercel.json? Those rules can affect the exact order of evaluation and might change the outcome for overlapping paths.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.