diff --git a/docs/middleware.md b/docs/middleware.md index 327f61179675e..2e717c262241c 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -30,6 +30,28 @@ export function middleware(req: NextRequest, ev: NextFetchEvent) { In this example, we use the standard Web API Response ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response)). +## API + +Middleware is created by using a `middleware` function that lives inside a `_middleware` file. Its API is based upon the native [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), and [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects. + +These native Web API objects are extended to give you more control over how you manipulate and configure a response, based on the incoming requests. + +The function signature: + +```ts +import type { NextFetchEvent } from 'next/server' +import type { NextRequest } from 'next/server' + +export type Middleware = ( + request: NextRequest, + event: NextFetchEvent +) => Promise | Response | undefined +``` + +The function can be a default export and as such, does **not** have to be named `middleware`. Though this is a convention. Also note that you only need to make the function `async` if you are running asynchronous code. + +**Note:** Edge Functions are currently in Beta. The API might change as we look to continually make improvements. + ## Examples Middleware can be used for anything that shares logic for a set of pages, including: