UndieGravity LogoUndiegravity

Next.js proxy.ts: The New Name for Middleware

Next.js 16 renames middleware.ts to proxy.ts. Learn what the proxy is for, how to redirect and protect routes with it, and what not to do inside it.

By UndieGravity, , 1 min read

If you upgraded to Next.js 16 and saw a deprecation warning about middleware, nothing is broken. The file is now called proxy.ts, and the function it exports is called proxy. The new name describes the job better: it sits in front of your routes and decides what happens to each request.

What the proxy is for

  • Redirecting users, for example from /login to /dashboard when they already have a session cookie.
  • Rewriting a URL to a different route without changing the address bar.
  • Setting or reading cookies and headers before a page renders.
  • Fast, coarse checks such as "is there a session cookie at all?"

A minimal proxy

// proxy.ts (project root, next to app/)
import { NextResponse, type NextRequest } from "next/server";

export function proxy(request: NextRequest) {
  const hasSession = request.cookies.has("session");
  if (!hasSession) {
    return NextResponse.redirect(new URL("/login", request.url));
  }
  return NextResponse.next();
}

export const config = {
  matcher: ["/dashboard/:path*", "/settings/:path*"],
};

The matcher limits the proxy to the routes that need it. Without it, the proxy runs on every request, including images and static files.

Migrating from middleware

  1. Rename middleware.ts to proxy.ts.
  2. Rename the exported function from middleware to proxy.
  3. Rename config flags that mention middleware, such as skipMiddlewareUrlNormalize to skipProxyUrlNormalize.

The proxy runs on the Node.js runtime and the runtime cannot be changed. If your middleware depends on the edge runtime, keep the old file name until you are ready to move.

What not to do in the proxy

  • Do not treat it as your only security check. A cookie being present does not mean it is valid. Verify the session again where the data is read: in the page, Server Action, or API.
  • Do not run slow work. Database queries and large fetches here delay every matched request.
  • Do not return page content. Redirect, rewrite, or pass the request on; let routes render pages.

A dashboard template usually needs only one thing from the proxy: send signed-out visitors to the login page. Keep it that small.

Keep reading

Next.js

Next.js 16: What Changed and How to Upgrade

Next.js 16 makes Turbopack the default, renames middleware to proxy, requires async request APIs, and changes caching. Here is what to update first.

, 2 min read