Performance: Why middleware intercepts requests
MEDIUM IMPACT
Middleware interception affects the time before the main page content starts loading by running code on requests early in the server process.
import { NextResponse } from 'next/server'; export function middleware(request) { // minimal quick check if (!request.cookies.get('auth')) { return NextResponse.redirect('/login'); } return NextResponse.next(); }
import { NextResponse } from 'next/server'; export function middleware(request) { // heavy synchronous logic for (let i = 0; i < 1000000; i++) {} return NextResponse.next(); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous middleware logic | 0 (server-side) | 0 | Blocks initial paint | [X] Bad |
| Minimal quick middleware checks | 0 (server-side) | 0 | Non-blocking initial paint | [OK] Good |