Next.js 16 renamed middleware.ts to proxy.ts: the errors you'll hit and how to fix them
DEV Community

Next.js 16 renamed middleware.ts to proxy.ts: the errors you'll hit and how to fix them

If you upgrade to Next.js 16 and still have a middleware.ts, the build prints this:

โš  The "middleware" file convention is deprecated. Please use "proxy" instead. To migrate automatically, run: npx @next/codemod@canary middleware-to-proxy . Learn more: https://nextjs.org/docs/messages/middleware-to-proxy

It still builds, so it's easy to ignore. But the rename is more than a new file name: the runtime changes too, and a few common setups break in ways the warning doesn't mention.

I tested every step below on a fresh create-next-app project with Next.js 16.4.6 and React 19.3.0, using next build and next start, plus curl and Playwright to check the responses. All errors are copied from real builds.

What changed in Next.js 16

From the proxy.js docs and the version 16 upgrade guide:

  • The file is now proxy.ts (or .js), in the same place as before: the project root, or src/ if you use it.
  • The function is exported as proxy (named) or as a default export.
  • Proxy always runs on the Node.js runtime. The runtime config option is not allowed.
  • Config flags with "middleware" in the name are renamed, for example skipMiddlewareUrlNormalize becomes skipProxyUrlNormalize.

The reason given in the docs: "middleware" was often confused with Express.js middleware, and the Next.js team wants this feature used as a last resort. "Proxy" describes what it is: a layer in front of your app that runs before routes render.

Migrate with the codemod

Commit your work first, then run:

npx @next/codemod@canary middleware-to-proxy .

On my test project it renamed middleware.ts to proxy.ts and renamed the function. Nothing else changed:

// proxy.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
  if (!request.cookies.has("session")) {
    return NextResponse.redirect(new URL("/login", request.url));
  }
}
export const config = {
  matcher: "/dashboard/:path*",
};

After next build and next start, the behavior was the same as before the rename: no cookie: 307 -> http://localhost:3100/login with cookie: 200

Two build errors when you rename by hand

If you only run mv middleware.ts proxy.ts and keep export function middleware, the build fails:

./proxy.ts Error: Proxy is missing expected function export name This function is what Next.js runs for every request handled by this proxy (previously called middleware). Rename the function to proxy.

A default export also works: export default function middleware() in proxy.ts built without errors in my test, but the upgrade guide recommends naming it proxy anyway.

The second error comes from copying the file instead of moving it, or from a merge that brings the old file back:

Error: Both middleware file "./middleware.ts" and proxy file "./proxy.ts" are detected. Please use "./proxy.ts" only. Delete middleware.ts.

You can't keep both.

Delete the runtime option

Many middleware files have runtime: "edge" or runtime: "nodejs" in their config. Both fail in proxy.ts.

With runtime: "edge":

Error: Next.js can't recognize the exported config field in route. Proxy does not support Edge runtime.

With runtime: "nodejs":

Error: Route segment config is not allowed in Proxy file at "./proxy.ts". Proxy always runs on Node.js runtime.

Keep only matcher in the config. If you need the Edge runtime, the upgrade guide says to keep using middleware.ts for now, since the Edge runtime is not supported in proxy.

Node.js APIs work now (a staging password example)

Because proxy runs on Node.js, you can import Node modules without any config. Here is a small password gate for a staging site using timingSafeEqual from node:crypto:

// proxy.ts
import { timingSafeEqual } from "node:crypto";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
function safeEqual(a: string, b: string) {
  const ab = Buffer.from(a);
  const bb = Buffer.from(b);
  return ab.length === bb.length && timingSafeEqual(ab, bb);
}
export function proxy(request: NextRequest) {
  const expected = "Basic " + Buffer.from(process.env.STAGING_AUTH ?? "").toString("base64");
  const given = request.headers.get("authorization") ?? "";
  if (!process.env.STAGING_AUTH || safeEqual(given, expected)) {
    return NextResponse.next();
  }
  return new NextResponse("Authentication required", {
    status: 401,
    headers: { "WWW-Authenticate": 'Basic realm="staging"' },
  });
}
export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};

I built without STAGING_AUTH, then started the server with STAGING_AUTH="team:s3cret" npx next start. The proxy read the variable at request time:

  • no auth: 401
  • wrong pass: 401
  • right pass: 200
  • css without auth: 200

When STAGING_AUTH is not set, the site stays open, so local development isn't blocked. Basic auth sends the password with every request, so use it only over HTTPS and only as a staging gate, not as your app's login.

For comparison, I put the same code in middleware.ts (function renamed back to middleware, no runtime option). The build passed with this warning:

Warning: A Node.js module is loaded ('node:crypto' at line 1) which is not supported in the Edge Runtime.

At runtime, every request returned 500, and the server log showed:

Error: Failed to load external module node:crypto: TypeError: Native module not found: node:crypto

So if your old middleware avoided Node APIs because of the Edge runtime, the rename to proxy lets you drop those workarounds.

Keep the proxy away from your CSS

This one isn't new in Next.js 16, but it's the bug I see most in auth redirects. Without a matcher, proxy runs on every request, including /_next/static files. This version redirects everything to /login:

// proxy.ts (broken: no matcher)
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
  const { pathname } = request.nextUrl;
  if (pathname !== "/login" && !request.cookies.has("session")) {
    return NextResponse.redirect(new URL("/login", request.url));
  }
}

The login page loads, but its stylesheet request gets redirected too. Playwright on /login:

  • CSS responses: [ '307 /next/static/chunks/0-ziarbb9nbg.css', '200 /login' ]
  • h1 computed: { fontSize: '32px', color: 'rgb(0, 0, 0)' }

The h1 has text-4xl text-blue-600, but it rendered in the browser's default 32px black because the CSS never arrived.

The fix is a negative matcher, as the docs recommend:

export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|webp)$).*)"],
};

After rebuilding:

  • CSS responses: [ '200 /next/static/chunks/0-ziarbb9nbg.css' ]
  • h1 computed: { fontSize: '36px', color: 'lab(44.0605 29.0279 -86.0352)' }

/dashboard still redirected to /login, and /next.svg and /favicon.ico returned 200.

One more point from the docs: Server Functions are POST requests to the route where they are used, so a matcher that skips a path also skips the Server Functions on it. Check auth inside each Server Function as well, not only in proxy.

Static export ignores proxy.ts

Proxy is not supported with output: "export". The build doesn't fail, though. With proxy.ts in place, it finished and printed:

โš  Statically exporting a Next.js application via next export disables API routes and middleware.

The out/ folder contained dashboard.html and no proxy code. On GitHub Pages, Cloudflare Pages or any static host, that "protected" page is public.

If you deploy a static export, move redirects and headers to your host's config (for example _redirects and _headers on Cloudflare Pages) and don't rely on proxy for anything.

Rename the config flags too

Old flag names don't break the build, but they print a warning. With skipMiddlewareUrlNormalize: true in next.config.ts:

โš  skipMiddlewareUrlNormalize is deprecated. Please use skipProxyUrlNormalize instead in next.config.ts.

Search your config for "middleware" and rename what you find.

Wrapping up

The short checklist:

  • Run npx @next/codemod@canary middleware-to-proxy . (or rename the file and the function yourself).
  • Delete the old middleware.ts.
  • Remove runtime from the config. If you need Edge, stay on middleware.ts for now.
  • Add a matcher that skips _next/static, _next/image and public files.
  • If you use static export, remember proxy doesn't run there.
  • Rename skipMiddlewareUrlNormalize and similar flags.

What did your middleware do that you're now moving to proxy: auth, redirects, i18n, or something else? I keep checks like these as free, MIT-licensed agent skills for Next.js launches: npx skills add nandot12/nextjs-launch-skills ยท GitHub

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.