mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
1c56ef613a
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.
37 lines
974 B
JavaScript
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()
|
|
}
|