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.
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>
Redirects and rewrites are processed in the following order:
_redirects
file. These are read in order until a match is found, then processing stops.
redirectsファイルにあるリダイレクトとリライト。これらは、一致するものが見つかるまで順番に読み込まれ、処理が停止する。netlify.toml
file. None of these are read if one previous rule has already matched.
netlify.tomlファイル内のリダイレクトと書き換え。これらは、一つ前のルールがすでにマッチしている場合には、読み込まれない。next.config.js
file).