Routing Middleware lets you to run code before your pages load, giving you control over incoming requests. It runs close to your users for fast response times and are perfect for redirects, authentication, and request modification.
Routing Middleware is available on the Node.js, and Edge runtimes. Node.js is the default runtime for Routing Middleware, to configure runtimes, see the runtimes documentation.
The following steps will guide you through creating your first Routing Middleware.
Create a file called middleware.ts
in your project root (same level as your package.json
) and add the following code:
export const config = {
runtime: 'nodejs', // defaults to 'edge'
};
export default function middleware(request: Request) {
console.log('Request to:', request.url);
return new Response('Logging request URL from Middleware');
}
Deploy your project and visit any page. You should see "Logging request URL from Middleware" instead of your normal page content.
To redirect users based on their URL, add a new route to your project called /blog
, and modify your middleware.ts
to include a redirect condition.
export const config = {
runtime: 'nodejs', // defaults to 'edge'
};
export default function middleware(request: Request) {
const url = new URL(request.url);
// Redirect old blog path to new one
if (url.pathname === '/old-blog') {
return new Response(null, {
status: 302,
headers: { Location: '/blog' },
});
}
// Let other requests continue normally
return new Response('Other pages work normally');
}
new URL(request.url)
to parse the incoming URL/old-blog
Location
header tells the browser where to goTry visiting /old-blog
- you should be redirected to /blog
.
By default, Routing Middleware runs on every request. To limit it to specific paths, you can use the config
object:
export const config = {
runtime: 'nodejs', // defaults to 'edge'
};
export default function middleware(request: Request) {
const url = new URL(request.url);
// Only handle specific redirects
if (url.pathname === '/old-blog') {
return new Response(null, {
status: 302,
headers: { Location: '/blog' },
});
}
return new Response('Middleware processed this request');
}
// Configure which paths trigger the Middleware
export const config = {
matcher: [
// Run on all paths except static files
'/((?!_next/static|_next/image|favicon.ico).*)',
// Or be more specific:
// '/blog/:path*',
// '/api/:path*'
],
};
matcher
array defines which paths trigger your Routing Middleware/blog/:path*
for specific sectionsSee the API Reference for more details on the config
object and matcher patterns.
When things don't work as expected:
console.log()
liberally and check your Vercel dashboard Logs tabrequest.headers
to see what's availableexport const config = {
runtime: 'nodejs', // defaults to 'edge'
};
export default function middleware(request: Request) {
// Debug logging
console.log('URL:', request.url);
console.log('Method:', request.method);
console.log('Headers:', Object.fromEntries(request.headers.entries()));
// Your functions logic here...
}
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