Middleware là middleware.ts ở root, chạy trên Edge runtime trước mọi request.
- Dùng để: authentication/authorization, redirect, rewrite URL, add/modify headers, A/B testing.
- Return NextResponse để điều khiển response.
- Dùng
matcherconfig để chỉ định routes áp dụng. - Nhẹ và nhanh vì Edge runtime.
Middleware is a middleware.ts file at the project root that runs on the Edge runtime before every request.
- Use it for: authentication/authorization checks, redirects, URL rewrites, adding/modifying response headers, and A/B testing.
- Return a NextResponse to control the response.
- Use the
matcherconfig to target specific routes. - It is fast and lightweight because it runs on the Edge.