Files
Wyatt Johnson 1c56ef613a feat: added rewrite headers after user-supplied rewrites (#74776)
Next.js's client router needs to know the pathname and query that the
request was rewritten to in order to facilitate reuse of static RSC
payloads generated from fallbacks. This takes the form of additional
headers being sent back on the response that includes the correct
rewritten pathname that later the client can take into account when
generating the client route key.
2025-01-13 16:15:43 -07:00

37 lines
974 B
JavaScript

import { NextResponse } from 'next/server'
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
}
export default function middleware(req) {
const url = new URL(req.url)
if (url.pathname === '/hello/wyatt') {
return NextResponse.rewrite(new URL('/hello/admin?key=value', url))
}
if (url.pathname === '/hello/bob') {
return NextResponse.rewrite(new URL('/hello/bobby', url))
}
if (url.pathname === '/hello/john') {
return NextResponse.rewrite(new URL('/hello/john?key=value', url))
}
if (url.pathname === '/hello/vercel') {
return NextResponse.rewrite('https://www.vercel.com')
}
return NextResponse.next()
}