What Is Next.js Middleware?
Next.js Middleware lets you run code before a request is completed. It runs on the Edge Runtime, enabling:
- Conditional redirects and rewrites
- Authentication and A/B testing at the edge
- Geo‑location based content
Getting Started
Create a `middleware.js` at your project root:
Best Practices
– Keep logic minimal to preserve cold‑start times.
– Use environment variables for secrets.
– Leverage `matcher` in `next.config.js` to scope your middleware only to necessary paths.