Moving Logic to the Edge

Origin web servers located in a single datacenter add 150ms+ latency for international users. Edge middleware runs on lightweight V8 isolates distributed across 300+ global edge locations.

// middleware.js
import { NextResponse } from 'next/server';

export function middleware(request) {
  const country = request.geo?.country || 'US';
  const response = NextResponse.next();
  response.headers.set('x-user-region', country);
  return response;
}