og-image.png

Netlify’s Next.js Runtime supports Next.js rewrites and redirects. These are defined in your next.config.js file and have some features that are not included in Netlify redirects and rewrites.

Use Netlify redirects and rewrites on a Next.js site

Every site on Netlify supports redirects and rewrites, which are defined in a _redirects or netlify.toml file. Sites that use Next.js Runtime are no exception.

However, there are some things to keep in mind when you use Netlify redirects and rewrites on a Next.js site. Next.js Runtime generates several rewrites on its own, which are used to map paths from your site to different Netlify Functions. The functions handle SSR, preview mode, and images, as well as assets in /_next/static.

Any Netlify redirects or rewrites that you create take precedence over those created by Next.js Runtime.

<aside> ⚠️ Avoid root-level rewrite

Do not add a rewrite from the site root (such as from = "/") in netlify.toml or _redirects. Your root-level rewrite would take precedence over Next.js Runtime’s generated rewrites and break routing on your site.

netlify.tomlや_redirectsに、サイトルートからの書き換え(from = "/"など)を追加しないでください。ルートレベルのリライトがNext.js Runtimeの生成したリライトよりも優先され、サイトのルーティングが壊れてしまいます。

</aside>

Redirect and rewrite precedence

Redirects and rewrites are processed in the following order:

  1. Redirects and rewrites in the _redirects file. These are read in order until a match is found, then processing stops. redirectsファイルにあるリダイレクトとリライト。これらは、一致するものが見つかるまで順番に読み込まれ、処理が停止する。
  2. Redirects and rewrites in the netlify.toml file. None of these are read if one previous rule has already matched. netlify.tomlファイル内のリダイレクトと書き換え。これらは、一つ前のルールがすでにマッチしている場合には、読み込まれない。
  3. At this point, if the request targets a static file, then the static file returns without further evaluation of Next.js redirects or rewrites. この時点で、リクエストが静的ファイルをターゲットにしている場合は、Next.jsのリダイレクトや書き換えを評価せずに、静的ファイルを返します。
  4. Any request that does not target a static file will then be passed to Next.js, which will then evaluate redirects and rewrites (defined in the next.config.js file).