Careeroza
What Is Middleware
Code Before Every Request
middleware.ts at the project root runs before a request is matched to a page or route handler. It can redirect, rewrite, set headers or cookies, or return a response directly.
// middleware.ts
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
console.log(request.method, request.nextUrl.pathname);
return NextResponse.next(); // continue to the page
}Renamed to proxy.ts in Next.js 16
Next.js 16 renames the file to proxy.ts and the function to proxy, to make clear that it sits in front of the app like a network proxy. It runs on the Node.js runtime by default. middleware.ts still works in 16 but is deprecated.
// proxy.ts (Next.js 16)
import { NextRequest, NextResponse } from 'next/server';
export function proxy(request: NextRequest) {
return NextResponse.next();
}Example
Migrate automatically:
npx @next/codemod@canary middleware-to-proxy .Example output
✓ Renamed middleware.ts -> proxy.ts
Keep It Light
Note: Middleware runs on every matching request, including prefetches. Do quick checks only (cookies, headers, redirects). Do not query the database for every request; do full authorization in the page, action or route handler too.