Guide
Middlewares

Middlewares

There are 4 types of middlewares that ca be defined to handle your requests:

Common Properties

These properties are available in all the middlewares types

interface BaseMiddleware {
    guard?: (params: UrlParams) => boolean;
    method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
    pre?: (request: NextRequestWithParams<T>) => MaybePromise<
        | boolean
        | {
              redirectTo: string | URL;
              statusCode?: number;
          }
    >;
}
propdesc
guardA function that checks if the given path arguments are valid. If false is returned then the middleware is skipped.
preThis is executed before the middleware. It can be useful to check authentication and separate the logic.
methodFilter the middleware by the HTTP method. If the method does not match the middleware is skipped.

⚠️ WARNING: The method property is not available on the HostnameMiddleware type.

Path Middleware

This is the standard middleware that is executed when the path is matched.

interface PathMiddleware<T> {
    handler: NextMiddlewareWithParams<T> | Middleware<T>[];
    path: PathMatcher;
    method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
    guard?: (params: UrlParams) => boolean;
    pre?: (request: NextRequestWithParams<T>) => MaybePromise<
        | boolean
        | {
              redirectTo: string | URL;
              statusCode?: number;
          }
    >;
}

Example

middleware.ts
const supportedLanguages = ["it", "en"]
const slugs = [...]
 
export default handlePaths([
  {
    path: "/:lang/blog/:slug",
    guard: params => supportedLanguages.includes(params.lang) || slugs.includes(params.slug),
    handler: (req, res, ev) => {
      // do something
 
      return res
    } 
  }
])

Redirect Middleware

The name is quite self-explanatory. This middleware is executed when the path is matched and it redirects to another path. If you need a more advanced redirect logic you can use the PathMiddleware

interface RedirectMiddleware<T> {
    path: PathMatcher;
    method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
    guard?: (params: UrlParams) => boolean;
    redirectTo: string | ((req: NextRequestWithParams<T>) => string);
    includeOrigin?: string | boolean;
}

Example Usage

middleware.ts
const supportedLanguages = ["it", "en"]
const slugs = [...]
 
export default handlePaths([
  {
    path: "/app",
    redirectTo: "/dashboard",
  }
])

Rewrite Middleware

This middleware is executed when the path is matched and it rewrites the path to another one.

interface RewriteMiddleware<T> {
    path: PathMatcher;
    method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
    guard?: (params: UrlParams) => boolean;
    rewriteTo: string | ((req: NextRequestWithParams<T>) => string);
}

Example Usage

middleware.ts
const supportedLanguages = ["it", "en"]
const slugs = [...]
 
export default handlePaths([
  {
    path: "/",
    rewriteTo: "/marketing",
  }, {
    path: "/dashboard",
    rewriteTo: "/app",
  }
])