What is @vercel/routing-utils?
@vercel/routing-utils is a utility package designed to help with routing configurations for Vercel deployments. It provides tools to define and manipulate routes, rewrites, redirects, and headers in a structured and efficient manner.
What are @vercel/routing-utils's main functionalities?
Define Routes
This feature allows you to define custom routes for your application. The 'src' field specifies the incoming request path, and the 'dest' field specifies the destination path.
{
"routes": [
{ "src": "/about", "dest": "/about.html" },
{ "src": "/blog/(.*)", "dest": "/blog/post?slug=$1" }
]
}
Rewrites
Rewrites allow you to map an incoming request path to a different destination path without changing the URL in the browser.
{
"rewrites": [
{ "source": "/old-path", "destination": "/new-path" }
]
}
Redirects
Redirects enable you to redirect an incoming request to a different URL with a specified status code, such as 301 for permanent redirects.
{
"redirects": [
{ "source": "/old-blog", "destination": "/new-blog", "statusCode": 301 }
]
}
Headers
This feature allows you to set custom headers for specific routes. The 'source' field specifies the route, and the 'headers' field contains an array of headers to be set.
{
"headers": [
{ "source": "/api/(.*)", "headers": [ { "key": "Cache-Control", "value": "no-cache" } ] }
]
}
Other packages similar to @vercel/routing-utils
next
Next.js is a React framework that provides built-in support for routing, rewrites, redirects, and headers. It offers a more comprehensive solution for building server-rendered React applications, including routing utilities similar to @vercel/routing-utils.
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It allows you to define routes, middleware, and custom headers, but requires more manual setup compared to @vercel/routing-utils.
koa
Koa is a new web framework designed by the team behind Express, aiming to be a smaller, more expressive, and more robust foundation for web applications and APIs. It provides similar routing capabilities but with a more modern and modular approach.
@vercel/routing-utils
Route validation utilities
Usage
yarn add @vercel/routing-utils
import { normalizeRoutes } from '@vercel/routing-utils';
const { routes, error } = normalizeRoutes(inputRoutes);
if (error) {
console.log(error.code, error.message);
}
import { routesSchema } from '@vercel/routing-utils';
const ajv = new Ajv();
const validate = ajv.compile(routesSchema);
const valid = validate([{ src: '/about', dest: '/about.html' }]);
if (!valid) console.log(validate.errors);