proxy.ts (formerly middleware)
Code that runs before a request is matched to a route.
- Renamed from middleware.ts to proxy.ts in Next 16; export a function named proxy.
- Great for optimistic redirects, rewrites, header injection and A/B bucketing.
- Use config.matcher to avoid running on every asset request.
- Do not rely on it alone for security: verify sessions near the data.
See it in this app: src/proxy.ts
export function proxy(req: NextRequest) {
if (!req.cookies.has("session")) {
return NextResponse.redirect(new URL("/login", req.url));
}
}
export const config = { matcher: ["/projects/:path*"] };