Middleware
Next.js Middleware runs before every request is completed, allowing you to modify the request and response. This is useful for adding security headers, logging, authentication, and more.
This site demonstrates middleware with several features:
- Security headers: Every response includes
X-Frame-Options,X-Content-Type-Options, andReferrer-Policyheaders - Request logging: All requests are logged with timestamp and method
- Path protection: Attempts to access
/adminpaths are automatically redirected to the homepage - API versioning: API routes receive an
X-API-Versionheader
Try accessing /admin to see the redirect in action. Check your browser's network inspector to see the security headers on this page's response.