|
| 1 | +# Redirects and rewrites |
| 2 | +Version 4 of the Essential Next.js build plugin adds support for native Next.js [rewrites](https://nextjs.org/docs/api-reference/next.config.js/rewrites) and [redirects](https://nextjs.org/docs/api-reference/next.config.js/redirects). These are defined in your `next.config.js` file and include support for some features that are not included in Netlify redirects and rewrites. |
| 3 | + |
| 4 | +## Using Netlify redirects and rewrites on a Next.js site |
| 5 | +Every site on Netlify supports [redirects and rewrites](https://docs.netlify.com/routing/redirects/), which are defined in a `_redirects` file or `netlify.toml`, and sites that use this plugin are no exceptions. However there are some caveats to bear in mind when using them. The plugin generates several rewrites of its own, which are used to map paths from the site to different Netlify functions which handle SSR, preview mode and images, as well as assets in `/_next/static`. Any Netlify redirects or rewrites that you create take precedence over these rewrites, so it is possible to break a site in that way. In particular, adding a root level rewrite means that no other rewrites will be applied to the site, meaning that your site will break. |
| 6 | + |
| 7 | +## Redirect and rewrite precedence |
| 8 | +Rewrites and redirects are applied in the following order: |
| 9 | + |
| 10 | +1. Redirects and rewrites in the `_redirects` file. These are read in order until a match is found, and then processing stops. |
| 11 | +2. Redirects and rewrites in the `netlify.toml` file. These are read in order until a match is found, and then processing stops. |
| 12 | +3. Auto-generated redirects and rewrites. These are generated by the plugin are required for the site to function. |
| 13 | +4. Next.js redirects and rewrites. These are defined in the `next.config.js` file, and are applied after all others have been processed. |
| 14 | + |
| 15 | +## General principles |
| 16 | + |
| 17 | +Netlify and Next.js redirects support different features and are evaluated at different points in the request lifecycle, so there are some considerations in deciding which one to use with your site. |
| 18 | + |
| 19 | +### When to use Netlify redirects or rewrites: |
| 20 | +- Generally if your redirect can be handled with Netlify redirects, these are faster to evaluate and should be preferred. |
| 21 | +- Features such as identity, proxying and country-based redirects are Netlify-specific and must use Netlify redirects. |
| 22 | +- If you need redirects or rewrites to be applied before loading static files, you must use Netlify redirects and rewrites. |
| 23 | + |
| 24 | +### When to use Next.js redirects or rewrites: |
| 25 | +- If you are using a _rewrite_ that points to a dynamic Next.js page, you must use Next.js rewrites. Next.js has no way of knowing what the rewritten page is when using Netlify rewrites, so the wrong page is likely to be rendered. This only applies to redirects, not rewrites. |
| 26 | +- If you need Next.js-specific features such as regex path or header matching, you must use Next.js rewrites. |
0 commit comments