Skip to the page

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.

The notes

What you must know

  • Rename: middleware.ts -> proxy.ts and the function middleware -> proxy (codemod npx @next/codemod@canary middleware-to-proxy .). middleware.ts still works for Edge but is deprecated.
  • Runtime: Node.js by default; setting the runtime config in proxy throws. One proxy file per project (split logic into imported modules).
  • Execution order: headers and redirects from next.config, then proxy, then beforeFiles rewrites, filesystem routes, afterFiles rewrites, dynamic routes, fallback rewrites.
  • Without a matcher it runs on every request including _next/static, _next/image and public/ files; use a negative-lookahead matcher. Proxy still runs for _next/data even 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-subrequest header 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

  1. In Next.js 16, middleware.ts became proxy.ts. What is it for, and what should never go in it?
  2. What runtime does it use, and can you change it?
  3. A March 2025 vulnerability let attackers skip middleware. What does that tell you about auth design?
  4. A locale redirect loops between / and /en. Debug it.
  5. 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 voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview