Routing Middleware

Routing Middleware is available on all plans

Routing Middleware executes code before a request is processed on a site, and are built on top of fluid compute. Based on the request, you can modify the response.

Because it runs globally before the cache, Routing Middleware is an effective way of providing personalization to statically generated content. Depending on the incoming request, you can execute custom logic, rewrite, redirect, add headers and more, before returning a response.

The default runtime for Routing Middlewares is Edge. See runtime options for information on how to change the runtime of your Routing Middleware.

You can use Routing Middleware with any framework. To add a Routing Middleware to your app, you need to create a middleware.ts file at your project's root directory.

middleware.ts
export default function middleware(request: Request) {
  const url = new URL(request.url);
 
  // Redirect old paths
  if (url.pathname === '/old-page') {
    return new Response(null, {
      status: 302,
      headers: { Location: '/new-page' },
    });
  }
 
  // Continue to next handler
  return new Response('Hello from your Middleware!');
}

The middleware.ts file should be at the same level as your app or pages directory (even if you're using a src directory). See the Quickstart guide for more information.

Routing Middleware has full support for the console API, including time, debug, timeEnd. Logs will appear inside your Vercel project by clicking View Functions Logs next to the deployment.

If your Routing Middleware depends on a database far away from one of our supported regions, the overall latency of API requests could be slower than expected, due to network latency while connecting to the database from an edge region. To avoid this issue, use a global database. Vercel has multiple global storage products, including Edge Config and Vercel Blob. You can also explore the storage category of the Vercel Marketplace to learn which option is best for you.

The following limits apply to requests processed by Routing Middleware:

NameLimit
Maximum URL length14 KB
Maximum request body length4 MB
Maximum number of request headers64
Maximum request headers length16 KB

Routing Middleware is available on the Node.js and Edge runtimes. The default runtime for Routing Middleware is Edge. You can change the runtime of your Routing Middleware by exporting a config object with a runtime property in your middleware.ts file.

middleware.ts
export const config = {
  runtime: 'nodejs', // defaults to 'edge'
};
export default function middleware(request: Request) {
  // Your middleware logic here
  return new Response('Hello from your Middleware!');
}

Routing Middleware is priced using the fluid compute model, which means you are charged by the amount of compute resources used by your Routing Middleware. See the fluid compute pricing documentation for more information.

The Vercel Observability dashboard provides visibility into your routing middleware usage, including invocation counts and performance metrics. You can get more insights with Observability Plus:

  • Analyze invocations by request path
  • Break down actions by type, such as redirects or rewrites
  • View rewrite targets and frequency
  • Use the query builder for custom insights

Learn more about Routing Middleware by exploring the following resources:

Last updated on June 25, 2025