Let’s talk about routing in Next.js. Today, we will talk about the one of most powerful things: middleware. Middleware in Next.js offers a powerful and flexible way both to intercept requests from the server and control request flow (redirects, URL rewriting) and globally enhance features like authentication, headers, and cookie persistence.
Creating Middleware
Let’s create a Middleware Next.js application. First of all, we’ll create a new file for middleware like middleware.js or middleware.ts, in the src folder. Middleware in Next.js then needs to allow you fine control over where it will be active (ie custom matcher configuration, or using isXXX functions)