Developing
Place a _middleware.ts file anywhere inside your app directory to create a middleware.
Here's an example:
Middlewares can be nested and will run from top to bottom on matching routes.
app/_middleware.ts
The createMiddleware function is a simple helper for types. Middlewares receive one argument, an object with three properties:
request is a Request for the current request.next is a function that returns a promise.context is an Object which you can mutate to pass data between middlewares.The next function will run the rest of the middlewares and then the current route, and will return the final Response. This lets you run some logic after the full route has been processed, and before it returns the final response.
Edit this page on GitHub.