NNext.js Feature Showcase
Server · Middleware

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

  1. 1The request for /blocked reaches the middleware first.
  2. 2Next.js gives the middleware the request's pathname.
  3. 3The pathname is /blocked, so it returns a redirect to /.
  4. 4The browser never reaches the blocked page.

Live result

Try to open /blocked

You should end up on the home page.