Capítulo 17 de 456
Proxy (renamed from Middleware in Next.js 16, same functionality) runs code before a request completes, letting you rewrite, redirect, modify headers, or respond directly based on the incoming request.
src/), same level as pages or app; only one is supported per project, but it can import from other modules.proxy export: Export as default or named proxy function; can be marked async.config.matcher: Filters which paths Proxy runs on, using path-matching patterns.fetch options cache, next.revalidate, next.tags have no effect inside Proxy.import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function proxy(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
export const config = {
matcher: '/about/:path*',
}
matcher restringindo o path de execução.redirects em next.config.ts quando não precisar de dados da request.matcher limita onde o Proxy roda; sem ele, roda em todas as rotas.proxy.ts por projeto; organize lógica em módulos importados.