The proxy file
proxy.ts replaces middleware.ts in Next.js 16. Redirect, rewrite, and set headers before a request finishes, scope it with a matcher, and know its limits.
The proxy file
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- In Next.js 16 the middleware file convention is renamed proxy: the file is proxy.ts and the exported function is proxy, with the same behavior as before.
- A proxy runs before a request completes and can redirect, rewrite, modify request or response headers, or respond directly.
- Without a matcher a proxy runs on every request, including static files and images, so scope it with matcher and exclude assets.
- Proxy defaults to the Node.js runtime, and setting a runtime option in it throws an error.
- Proxy is for quick optimistic checks, not slow data fetching or full authorization; Server Functions are POST requests to their page's route, so a matcher exclusion also skips them.
A checkpoint in front of the app
A request arrives. Before any page or handler runs, you sometimes want to act: send logged-out visitors to /login, rewrite / to a variant for an A/B test, add a header. A proxy is that checkpoint, a function that runs for matching requests before they reach your routes.
The name changed in 16. The docs explain the rename: “middleware” was often confused with Express middleware, and the term “proxy” better describes a network boundary in front of the app. The functionality remains the same.
The file
// proxy.ts (project root, or src/ if you use it)
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function proxy(request: NextRequest) {
const response = NextResponse.next()
response.headers.set('x-from-proxy', '1')
return response
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
}
Only one proxy.ts is supported per project; split logic into modules and import them. You can export proxy by name or as the default. I built this file on 16.4.6 and next build listed it as ƒ Proxy (Middleware) in the route output.
From the function you can redirect (NextResponse.redirect), rewrite (NextResponse.rewrite), continue with changed headers, or return a response yourself.
To migrate old code, run the codemod from the docs: npx @next/codemod@canary middleware-to-proxy .. It renames the file and the function.
Scope it with a matcher
Without a matcher, proxy runs on every request, including _next/static, image optimization, and files in public/. The docs warn that auth logic or redirects can then block CSS, JS, or images from loading. The matcher above uses a negative lookahead to skip those. Matcher values must be constants so they can be analyzed at build time.
Even when _next/data is excluded by a negative matcher, proxy still runs for those routes. The docs say this is intentional, to avoid protecting a page but forgetting its data route.
Order of events
The docs list the order: headers from config, redirects from config, then Proxy, then beforeFiles rewrites, filesystem routes, afterFiles rewrites, dynamic routes, and fallback rewrites. That is why simple fixed redirects belong in next.config.ts first: they run earlier and cost nothing per request. Use a proxy when you need request data or more logic.
Limits worth knowing
- Runtime. Proxy uses the Node.js runtime by default. A
runtimeoption is not available in proxy files, and setting one throws. - No slow fetching. The docs say proxy is not intended for slow data fetching.
- Caching directives do nothing here.
use cacheanduse serverhave no effect in proxy, andfetchcache options do not cache responses. revalidateTagcannot be called from proxy.
Proxy is not your security layer
Optimistic checks are a good fit: read a session cookie, and if it is absent, redirect. They are fast and help UX. They are not authorization. The docs call out a subtle hole: Server Functions are not separate routes in the proxy chain; they are POST requests to the route where they are used. A matcher that skips a path skips actions on it too, and moving an action to another route can silently remove coverage. The conclusion in the docs is to verify authentication and authorization inside each action and data function. Protecting routes and data builds on this.
What the video covers
Shiraz Dev’s video is from November 2025. Per its description, it explains how the new proxy.ts (or proxy.js) convention works, why it replaces middleware.ts, how to set it up, and use cases such as redirects, rewrites, and header and cookie manipulation. It has no chapters, so this lesson does not claim more than that description. The linked ByteGrad runner-up is about where authentication belongs, proxy or data access layer, and carries a paid Kinde sponsorship.
Where this fits
That completes mutations and APIs: actions for your UI, handlers for other callers, and a proxy in front of both. Next up is how pages get rendered, starting from the basics: static and dynamic rendering.
Check yourself
What changed between middleware.ts and proxy.ts in Next.js 16?
The file and exported function are renamed from middleware to proxy and the functionality is the same, and a codemod, npx @next/codemod@canary middleware-to-proxy ., does the rename.
Why do you almost always add a matcher to a proxy?
Without one it runs on every request, including _next/static, images, and public files, so logic like auth redirects can block CSS and JavaScript from loading.
Why is a proxy a poor place for your only authorization check?
It is meant for fast optimistic checks, a matcher change or a moved Server Function can silently drop coverage, and Server Functions are handled as POST requests to the page's route, so each action must still check for itself.
When should you use next.config redirects instead of a proxy?
For simple fixed redirects, because the docs recommend redirects in configuration first and say a proxy is for needing request data or more complex logic.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.