Next.jsMedium

What are the limitations of Next.js middleware?

Proxy (formerly middleware) runs before routing on every matched request, so it must be fast and stateless. It cannot render React, cannot use the caching directives or fetch cache, should not do slow data fetching, has only one file per project, and must not be your only authorization layer.

Functional limits

  • One file: a single proxy.ts; compose logic inside it.
  • No React rendering: it returns responses, redirects or rewrites, not components.
  • No directive transforms: "use cache", "use server" and cacheLife/cacheTag have no effect (calling cacheLife throws).
  • No fetch caching: cache, next.revalidate and next.tags do nothing; use an external store.
  • Static matchers: config.matcher must be constant.
  • Runtime fixed to Node.js in proxy.ts; the runtime option throws. Edge needs the deprecated middleware.ts.
  • Request body: Proxy can read bodies, but there is a size limit (proxyClientMaxBodySize) and buffering bodies slows every request.

Design limits

  • Latency: it adds work to every matched request, including prefetches and RSC navigations. Keep it to cookie checks and lookups in fast stores.
  • No shared state with the app: it may be deployed separately, so module globals are not shared. Pass data through headers, cookies or the URL.
  • Not a security boundary on its own: Server Functions on excluded paths skip it, and refactors change paths. Check auth in the data layer.
  • Internal headers stripped: RSC headers like rsc and next-router-state-tree are hidden from request.headers.

When to use something else

  • Static redirects and headers: next.config.ts.
  • Per-page auth checks and data: Server Components and the Data Access Layer.
  • Heavy processing: Route Handlers or background jobs with after().

Code example

tsx
1// proxy.ts - what works and what does not
2import { NextResponse, type NextRequest, type NextFetchEvent } from "next/server";
3import { redis } from "@/lib/redis"; // fast external store: OK
4
5export async function proxy(request: NextRequest, event: NextFetchEvent) {
6 // OK: cheap cookie check
7 if (!request.cookies.has("session") && request.nextUrl.pathname.startsWith("/app")) {
8 return NextResponse.redirect(new URL("/login", request.url));
9 }
10
11 // OK: fast key-value lookup for dynamic redirects
12 const target = await redis.get<string>(`redirect:${request.nextUrl.pathname}`);
13 if (target) return NextResponse.redirect(new URL(target, request.url), 308);
14
15 // OK: background work after the response
16 event.waitUntil(fetch("https://logs.example.com", { method: "POST", body: request.url }));
17
18 // Not effective here:
19 // await fetch(url, { next: { revalidate: 60 } }); // not cached in Proxy
20 // "use cache" / cacheLife() // no effect / throws
21 // await db.user.findMany() // too slow for every request
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app