Rescale logoNEMO

Functions

Middleware functions standardization, explanation and usage

This package introduces middleware functions standardization for more elastic approach to development and feature delivery.

Function schema

This example shows all props that are provided for your usage.

_middleware.ts
async ({ request, response, context, event }: MiddlewareFunctionProps) => {
  return response
}
PropTypeDefault
request
NextRequest
-
response
NextResponse<unknown>
-
context
Map<string, unknown>
-
event
NextFetchEvent
-

Every function should return an response object (pass from props or return custom).

Response object should be compatible with Response or NextResponse interface

Explanation

Prop:  request

Type: NextRequest

That's a user middleware's request passed to function, which can be later updated by functions in chain.

For example in case of authorization processes or custom headers/cookies management this prop can differ from its entry form.

Prop:  response

Type: NextResponse

This property contains NextResponse.next() (only for first executed function in chain), which can be updated by other functions in chain.

Prop:  context

Type: Map<string, unknown>

This property contains context shared across whole execution chain for every function.

Shared context

Learn more about Shared Context in middleware

If you want to know more about Map interface usage please refer to these docs.

Prop:  event

Type: NextFetchEvent

This property contains event object for serverless functions execution.

It can be used to use Next.js 15 new features like event.waitUntil().

You can read more there: Next.js 15 waitUntil

Last updated on

On this page