---
title: "How do I migrate middleware.ts to proxy.ts in Next.js 16?"
description: "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."
url: https://www.danny.engineering/answer/nextjs/how-do-i-migrate-middlewarets-to-proxyts-in-nextjs-16
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["Next.js"]
---

# How do I migrate middleware.ts to proxy.ts in Next.js 16?

## 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.


```bash
npx @next/codemod@latest middleware-to-proxy .
```


```diff
-export function middleware(request: NextRequest) {
+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.


```bash
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`:


```bash
mv middleware.ts proxy.ts
```


```typescript
// 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));
  }
  return NextResponse.next();
}

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


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`:


```typescript
// next.config.ts
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  skipProxyUrlNormalize: true,
};

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:


```typescript
// proxy.ts
import { withAuth } from './lib/with-auth';

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.

## Related reading

- [Next.js: proxy.js file convention](https://nextjs.org/docs/app/api-reference/file-conventions/proxy)
- [Next.js: upgrading to version 16](https://nextjs.org/docs/app/guides/upgrading/version-16)
