HomeToolsAbout

Middleware

What is it

Middleware intercepts request or response before they are passed to application's core code.

  • e.g. authentication to verify a user's identity before granting access.

Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly

  • Middleware runs before cached content and routes are matched

How is it used

You can set CORS headers in Middleware to allow cross-origin requests

  • including preflight and simple requests

Where to Place it

Use the file middleware.ts in the root of your project to define Middleware

  • For example, at the same level as pages or app, or inside src if applicable

matcher

matcher allows you to filter Middleware to run on specific paths

  • you can also use regex
export const config = { matcher: [ '/about/:path*', '/dashboard/:path*' ], }
AboutContact