More Next.js answers

by Danny
Published 2 min read

Next.js 16 renames middleware to proxy. Rename the file and the exported function, update renamed config flags, and check you were not relying on the edge runtime.

The short answer

#

Rename middleware.ts to proxy.ts, keep it in the same place, and rename the exported function from middleware to proxy. The official codemod does both for you. Your logic, matcher config and NextResponse calls stay the same. The one real change is that proxy always runs on the Node.js runtime, so if you depend on the edge runtime, keep middleware.ts for now.


_10
npx @next/codemod@latest middleware-to-proxy .


_10
-export function middleware(request: NextRequest) {
_10
+export function proxy(request: NextRequest) {

Why it happens

#

In Next.js 16 the middleware file convention is deprecated and has been renamed to proxy. The Next.js docs give two reasons. The word "middleware" was often confused with Express middleware, which encouraged people to put far too much in it. And "proxy" better describes what the file does: it sits at a network boundary in front of your app and handles a request before it reaches your routes.

The Next.js team also recommends treating proxy as a last resort, and has been adding APIs that cover common uses without it.

The rename comes with a runtime change. Proxy uses the Node.js runtime, and the runtime option cannot be set in a proxy file; setting it throws an error. The edge runtime is not supported in proxy.

How to fix it

#

Run the codemod

The middleware-to-proxy codemod renames the file and the named export, and updates config options that contained the word middleware. The general upgrade codemod, npx @next/codemod@canary upgrade latest, includes this step too.


_10
npx @next/codemod@latest middleware-to-proxy .

Or rename it by hand

The file goes in the project root, or inside src if you use one, at the same level as app or pages:


_10
mv middleware.ts proxy.ts


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

The file must export a single function, either named proxy or as the default export. The docs recommend naming it proxy even when you use a default export.

Rename config flags

Options in next.config.ts that mentioned middleware have new names. For example, skipMiddlewareUrlNormalize is now skipProxyUrlNormalize:


_10
// next.config.ts
_10
import type { NextConfig } from 'next';
_10
_10
const nextConfig: NextConfig = {
_10
skipProxyUrlNormalize: true,
_10
};
_10
_10
export default nextConfig;

The codemod also renames the experimental middlewarePrefetch, middlewareClientMaxBodySize and externalMiddlewareRewritesResolve options to their proxy equivalents.

Remove any runtime setting

If your middleware file exported a runtime value, delete it from proxy.ts. If your code genuinely needs the edge runtime, do not rename the file yet. middleware.ts still works in Next.js 16 while it is deprecated, and the Next.js team has said further edge runtime guidance will follow in a minor release.

Wrap helper functions from libraries

If a library gives you a ready-made middleware function, export it under the new name:


_10
// proxy.ts
_10
import { withAuth } from './lib/with-auth';
_10
_10
export const proxy = withAuth;

Common pitfalls

#
  • Blindly renaming edge middleware. Check for edge-only code or a runtime export before you switch.
  • Putting proxy.ts inside app. It must sit beside app, in the root or in src.
  • Leaving the old middleware.ts in place after creating proxy.ts. Delete it so there is one clear entry point.
  • Using proxy as your only auth check. Server Functions are handled as POST requests to the route that uses them, so a matcher change can silently remove coverage. Verify the user inside each Server Function and data access function too.
  • Forgetting a matcher. Without one, proxy runs on every request, including static files and images.
#

Was this resource helpful?