In this article, we describe the following methods to redirect a path to another location by using Vercel and Next.js features:
If you need to define more than 2048 redirects, you can use bulk redirects on Vercel to serve a large number of redirects performantly from the edge.
You can define redirects on Vercel using the vercel.json or vercel.ts project configuration. If you are using a framework, such as Next.js, you can also define the redirects in the framework-specific configuration file.
You can define up to 2048 routes in the Vercel configuration. If you need to define more redirects, you can use bulk redirects, or look at Edge Middleware or a Serverless Function.
The Regex Path Matching documentation explains the syntax for source and destination fields.
vercel.ts is an alternative to vercel.json that allows you to write the Vercel configuration using TypeScript. Learn more in the vercel.ts documentation.
If you are deploying a Next.js application to Vercel, you can define redirects in the following ways:
- Using the
redirectsconfiguration. - Using a Next.js page.
- Using an API route.
The main characteristic of the redirects feature is that it is a build-time configuration. Which means it is only possible to generate it once before the application is deployed.
You can check the official Next.js documentation for instructions on how to define the redirects statically.
Performing redirects using a Next.js page gives your deployment the ability to fallback to a normal page with a presentable UI in case the redirect is not found. Therefore, using a page for redirects is recommended if you need to dynamically query an external resource to fetch your list of redirects, and those routes will be shared with your users. To avoid executing the Serverless Function for every request to a certain path, it is also recommended to cache the response.
Now that we have a page ready to accept requests and redirect given a condition, it is also necessary to route all relevant paths to the page above. In the code snippet below, we will use rewrites to adjust our routing behavior.
You can also perform redirects using an API Route. This method is recommended if you need to dynamically query an external resource to fetch your list of redirects, and you want an endpoint that can perform those redirects without a presentable page. To avoid invoking the Serverless Function for every request to a certain path, it is also recommended to cache the response.
With the API route ready to accept requests and perform redirects, we need to configure the routing configuration of the Next.js deployment. In the code snippet below, we will use rewrites to adjust our routing behavior.
Using the Next.js routing methods above you can optimize your routes by implementing a catch-all fallback to handle simpler redirects avoiding the 2048 limit yet keeping your redirects intact.
During the build phase, we split redirects into two categories: simple ones that can be handled by an API route (with no route limits), and complex rules that are defined in a next.config.js file (where there is a route limit). For a full example project, you can check out our public repository here.
At the moment, there is a limit of 2048 redirect rules that can be described in the configuration for Vercel deployed apps. To effectively bypass the limit 2048 "Routes created per Deployment" limit, it is advised to migrate your static redirects to Edge Middleware.
Edge Middleware can also be paired with Edge Config where a JSON blob of redirect rules can be stored and read from.
You can perform redirects with a Serverless Function. This method is recommended if you need to dynamically query an external resource to fetch your list of redirects. To avoid invoking the Serverless Function for every request to a certain path, it is also recommended to cache the response.
We have created a Node.js Serverless Function that accepts requests and perform redirects programatically. The next step should be to create a new rewrite rule to adjust our routing behavior.
As outlined in this article, there are multiple ways to perform redirects when deploying an application to Vercel. If you are using Next.js, it is recommended that you use the next.config.js configuration if you need statically defined rewrites. If you need to query external resources or perform server-side computation, using a Next.js page or an API Route gives you the required flexibility for advanced use-cases. For other frameworks, you can either define statically your redirects with vercel.json or use Serverless Functions for advanced requirements.