Middleware: route blocking
Middleware runs before a request reaches a page. Here,
middleware.ts checks the address and, if it's /blocked, redirects to the home page instead.Expected behaviour
Click the link: you'll land back on the home page, because the middleware intercepts the request and sends a redirect. Useful for protecting routes, redirecting by sign-in or role, or retiring old pages.
How this page works
- 1The request for
/blockedreaches the middleware first. - 2Next.js gives the middleware the request's pathname.
- 3The pathname is
/blocked, so it returns a redirect to/. - 4The browser never reaches the blocked page.
Live result
Try to open /blocked
You should end up on the home page.