Edge Middleware runs before your page is rendered, allowing you to modify requests, add headers, implement redirects, and perform authentication - all at the edge for maximum performance.
USUnknownOpenNext-Middleware2025-12-13T04:00:25.973ZCheck user tokens, redirect to login pages, and protect routes before they load.
Detect user location and redirect to appropriate regional sites or languages.
Add security headers, implement rate limiting, and block malicious requests.
The middleware for this demo is located in src/middleware.ts. It demonstrates geolocation detection, header injection, and security enhancements.
// Example middleware functionality
export function middleware(request: NextRequest) {
const country = request.headers.get('cf-ipcountry');
const response = NextResponse.next();
// Add custom headers
response.headers.set('x-user-country', country);
// Security headers
response.headers.set('x-frame-options', 'DENY');
return response;
}