Performance: Middleware.ts file convention
MEDIUM IMPACT
Middleware.ts affects the initial request handling and routing speed, impacting how fast the server responds and the page starts loading.
import { NextRequest, NextResponse } from 'next/server'; export function middleware(request: NextRequest) { if (!request.cookies.get('token')) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); }
import { NextRequest, NextResponse } from 'next/server'; export function middleware(request: NextRequest) { // Heavy synchronous computation for (let i = 0; i < 1000000000; i++) {} if (!request.cookies.get('token')) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); }
| Pattern | Server Blocking Time | Middleware Complexity | Impact on LCP | Verdict |
|---|---|---|---|---|
| Heavy synchronous logic in middleware | 200+ ms | High | Delays LCP significantly | [X] Bad |
| Minimal logic with quick checks | Under 5 ms | Low | Improves LCP | [OK] Good |