← Back to Home
🎯

Edge Middleware Demo

🎯 What is Edge Middleware?

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.

Headers Added by Middleware

Country:US
City:Unknown
Processed by:OpenNext-Middleware
Timestamp:2025-12-13T04:00:25.973Z

Middleware Capabilities

  • Geolocation-based routing
  • Authentication checks
  • Security headers injection
  • A/B testing logic
  • Request/response modification

Common Middleware Use Cases

🔐 Authentication

Check user tokens, redirect to login pages, and protect routes before they load.

🌍 Localization

Detect user location and redirect to appropriate regional sites or languages.

🛡️ Security

Add security headers, implement rate limiting, and block malicious requests.

🔍 View Middleware Code

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;
}