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.
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:
Name Limit Maximum URL length 14 KB Maximum request body length 4 MB Maximum number of request headers 64 Maximum request headers length 16 KBRouting 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.
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:
Learn more about Routing Middleware by exploring the following resources:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4