Skip to main content

What is middleware?

In Next.js, middleware refers to a concept where you can intercept and modify the incoming HTTP requests before they reach the actual route handlers. It allows you to perform tasks such as authentication, logging, or data manipulation, and then pass the request along to the appropriate route handler. Middleware acts as a bridge between the server and the route handlers, providing a way to add additional functionality to an application's request handling pipeline.

In Blueprint's architecture middleware is used as a transitional layer. It is used to intercept incoming requests using matchers and can modify or redirect the response as needed. This is done by using the middleware.ts file at the root of the application.

How is middleware used in Blueprint?

Middleware in Blueprint is currently used exclusively for authentication purposes, including:

  • Authentication: Ensuring only users with validated authentication are able to access protected routes.
  • Expiring URL's: Providing a method of authenticating Expiring URL's with URL parameters, prior to page rendering.
  • Masquerading: Providing a method of authenticating administrators masquerading, using URL parameters, prior to page rendering.

The configuration includes matchers that are designed to match any paths that require authentication prior to rendering.

Every intercepted middleware route is responsible for checking if the necessary cookies are present in each request and either setting the appropriate secure cookie or redirecting.

Shared middleware

In Blueprint, middleware functions can be shared across multiple applications. This allows for code reuse and promotes consistency in handling common tasks.

Each middleware function is located within the middlewares folder of packages/core. By centralising these functions, they can be easily accessed and utilised by multiple applications within the Blueprint ecosystem.

Overall, the use of shared middleware functions in Blueprint enhances code reusability, promotes consistency, and improves the overall development experience.

Edge Runtime

edge-middleware-diagram Edge Middleware location within Vercel infrastructure. Image courtesy of vercel.com

Edge Middleware is a serverless compute environment provided by Vercel. This feature is used to ensure high performance and low latency in each Blueprint application.

Edge Middleware operates at the edge of the network, closer to the user, rather than running on the server or client. This allows for faster processing and response times. It intercepts incoming requests and can modify or redirect them as needed.

By utilising Edge Middleware, Blueprint is able to optimise the delivery of content and improve the overall user experience. It plays a crucial role in enhancing performance and reducing latency of the application.

It is pertinent to note that using Node.js modules directly or importing files that use Node.js imports will result in errors when running the build command for the applications.