Mutations, APIs and the request layer · senior
proxy.ts (formerly middleware.ts)
In Next.js 16 middleware.ts was renamed proxy.ts; it runs on the Node.js runtime before routing for every matched request and is for redirects, rewrites, headers and cheap optimistic checks, never the only security layer and never slow data fetching.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Rename:
middleware.ts->proxy.tsand the functionmiddleware->proxy(codemodnpx @next/codemod@canary middleware-to-proxy .).middleware.tsstill works for Edge but is deprecated. - Runtime: Node.js by default; setting the
runtimeconfig in proxy throws. One proxy file per project (split logic into imported modules). - Execution order:
headersandredirectsfrom next.config, then proxy, thenbeforeFilesrewrites, filesystem routes,afterFilesrewrites, dynamic routes,fallbackrewrites. - Without a
matcherit runs on every request including_next/static,_next/imageandpublic/files; use a negative-lookahead matcher. Proxy still runs for_next/dataeven if excluded. - Good uses: redirects/rewrites, locale detection, A/B tests, setting headers, optimistic auth (is there a session cookie?), bot routing.
- Not for: full authorisation, database calls (it runs on prefetches too), or caching:
"use cache"and fetch cache options have no effect in proxy. - CVE-2025-29927 (March 2025): the
x-middleware-subrequestheader let attackers skip middleware on self-hosted apps; fixed in 15.2.3/14.2.25. Lesson: defence in depth, real checks in the data layer. - Server Functions are POSTs to the page route: a matcher that excludes a path also skips its actions.
The red pen
Common traps: mistake → fix
Mistake: "Middleware/proxy runs on the Edge by default"
Fix: proxy runs on Node.js in 16.
Mistake: "Proxy is enough to protect admin routes"
Fix: it can be bypassed or misconfigured; authorise in the DAL.
Mistake: "Use
'use cache'to speed up proxy lookups"Fix: directives do nothing in proxy.
Easy to hard
Questions interviewers ask, easy to hard
- In Next.js 16,
middleware.tsbecameproxy.ts. What is it for, and what should never go in it? - What runtime does it use, and can you change it?
- A March 2025 vulnerability let attackers skip middleware. What does that tell you about auth design?
- A locale redirect loops between
/and/en. Debug it. - How do you stop proxy from running on static assets?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.