rewrites
重写允许你将传入的请求路径映射到不同的目标路径。
重写充当 URL 代理并掩盖目标路径,使其看起来用户没有改变他们在网站上的位置。相比之下,redirects 会重新路由到新页面并显示 URL 变化。
要使用重写,你可以在 next.config.js 中使用 rewrites 键:
module.exports = {
async rewrites() {
return [
{
source: '/about',
destination: '/',
},
]
},
}重写应用于客户端路由。在上面的例子中,导航到 <Link href="/about"> 将提供来自 / 的内容,同时保持 URL 为 /about。
rewrites 是一个异步函数,期望返回一个数组或对象数组(见下文),其中包含具有 source 和 destination 属性的对象:
source:String- 是传入请求路径模式。destination:String是你想要路由到的路径。basePath:false或undefined- 如果为 false,则在匹配时不包含 basePath,仅可用于外部重写。locale:false或undefined- 匹配时是否不包含 locale。has是一个 has 对象 数组,具有type、key和value属性。missing是一个 missing 对象 数组,具有type、key和value属性。
当 rewrites 函数返回数组时,重写在检查文件系统(pages 和 /public 文件)之后、动态路由之前应用。当 rewrites 函数返回具有特定形状的对象数组时,从 Next.js 的 v10.1 开始,可以更改并更精细地控制此行为:
module.exports = {
async rewrites() {
return {
beforeFiles: [
// 这些重写在 headers/redirects 之后
// 以及所有文件(包括 _next/public 文件)之前检查
// 允许覆盖页面文件
{
source: '/some-page',
destination: '/somewhere-else',
has: [{ type: 'query', key: 'overrideMe' }],
},
],
afterFiles: [
// 这些重写在 pages/public 文件
// 检查之后、动态路由之前检查
{
source: '/non-existent',
destination: '/somewhere-else',
},
],
fallback: [
// 这些重写在 pages/public 文件
// 和动态路由都检查之后检查
{
source: '/:path*',
destination: `https://my-old-site.com/:path*`,
},
],
}
},
}值得注意的是:
beforeFiles中的重写在匹配 source 后不会立即检查文件系统/动态路由,它们会继续,直到所有beforeFiles都被检查完毕。
Next.js 路由检查的顺序是:
- headers 被检查/应用
- redirects 被检查/应用
- proxy
beforeFiles重写被检查/应用- 来自 public 目录、
_next/static文件和非动态页面的静态文件被检查/提供 afterFiles重写被检查/应用,如果这些重写之一被匹配,我们会在每次匹配后检查动态路由/静态文件fallback重写被检查/应用,这些在渲染 404 页面之前、动态路由/所有静态资源都被检查之后应用。如果你在getStaticPaths中使用 fallback: true/'blocking',在你的next.config.js中定义的 fallbackrewrites将_不会_运行。
重写参数
在重写中使用参数时,如果在 destination 中没有使用任何参数,参数将默认在查询中传递。
module.exports = {
async rewrites() {
return [
{
source: '/old-about/:path*',
destination: '/about', // :path 参数在这里没有使用,所以会自动在查询中传递
},
]
},
}如果在目标中使用了参数,则不会在查询中自动传递任何参数。
module.exports = {
async rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*', // :path 参数在这里使用,所以不会自动在查询中传递
},
]
},
}如果目标中已经使用了一个参数,你仍然可以通过在 destination 中指定查询来手动在查询中传递参数。
module.exports = {
async rewrites() {
return [
{
source: '/:first/:second',
destination: '/:first?second=:second',
// 由于 :first 参数在目标中使用,:second 参数
// 不会自动添加到查询中,但我们可以手动添加它
// 如上所示
},
]
},
}路径匹配
允许路径匹配,例如 /blog/:slug 将匹配 /blog/hello-world(无嵌套路径):
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug',
destination: '/news/:slug', // 匹配的参数可以在目标中使用
},
]
},
}通配符路径匹配
要匹配通配符路径,你可以在参数后使用 *,例如 /blog/:slug* 将匹配 /blog/a/b/c/d/hello-world:
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*', // 匹配的参数可以在目标中使用
},
]
},
}正则表达式路径匹配
要匹配正则表达式路径,你可以在参数后用括号包裹正则表达式,例如 /blog/:slug(\\d{1,}) 将匹配 /blog/123 但不匹配 /blog/abc:
module.exports = {
async rewrites() {
return [
{
source: '/old-blog/:post(\\d{1,})',
destination: '/blog/:post', // 匹配的参数可以在目标中使用
},
]
},
}以下字符 (、)、{、}、[、]、|、\、^、.、:、*、+、-、?、$ 用于正则表达式路径匹配,因此当在 source 中用作非特殊值时,必须通过在它们前面添加 \\ 来转义:
module.exports = {
async rewrites() {
return [
{
// 这将匹配请求 `/english(default)/something`
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
},
]
},
}Header、Cookie 和 Query 匹配
仅当 header、cookie 或 query 值也匹配 has 字段或不匹配 missing 字段时才匹配重写。source 和所有 has 项必须匹配,并且所有 missing 项必须不匹配,才能应用重写。
has 和 missing 项可以具有以下字段:
type:String- 必须是header、cookie、host或query。key:String- 要匹配的所选类型的键。value:String或undefined- 要检查的值,如果为 undefined,则任何值都将匹配。可以使用类似正则表达式的字符串来捕获值的特定部分,例如,如果first-(?<paramName>.*)用于first-second,则second将在目标中可用,使用:paramName。
module.exports = {
async rewrites() {
return [
// 如果 header `x-rewrite-me` 存在,
// 将应用此重写
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// 如果 header `x-rewrite-me` 不存在,
// 将应用此重写
{
source: '/:path*',
missing: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// 如果 source、query 和 cookie 匹配,
// 将应用此重写
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// page 值在目标中不可用
// 因为提供了 value 并且没有
// 使用命名捕获组,例如 (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
destination: '/:path*/home',
},
// 如果 header `x-authorized` 存在并且
// 包含匹配的值,将应用此重写
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
destination: '/home?authorized=:authorized',
},
// 如果主机是 `example.com`,
// 将应用此重写
{
source: '/:path*',
has: [
{
type: 'host',
value: 'example.com',
},
],
destination: '/another-page',
},
]
},
}重写到外部 URL
示例
重写允许你重写到外部 URL。这对于逐步采用 Next.js 特别有用。以下是将主应用的 /blog 路由重定向到外部站点的示例重写。
module.exports = {
async rewrites() {
return [
{
source: '/blog',
destination: 'https://example.com/blog',
},
{
source: '/blog/:slug',
destination: 'https://example.com/blog/:slug', // 匹配的参数可以在目标中使用
},
]
},
}如果你使用 trailingSlash: true,你还需要在 source 参数中插入尾部斜杠。如果目标服务器也期望尾部斜杠,它也应该包含在 destination 参数中。
module.exports = {
trailingSlash: true,
async rewrites() {
return [
{
source: '/blog/',
destination: 'https://example.com/blog/',
},
{
source: '/blog/:path*/',
destination: 'https://example.com/blog/:path*/',
},
]
},
}逐步采用 Next.js
你还可以让 Next.js 在检查所有 Next.js 路由后回退到代理到现有网站。
这样,在将更多页面迁移到 Next.js 时,你不必更改重写配置
module.exports = {
async rewrites() {
return {
fallback: [
{
source: '/:path*',
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
},
],
}
},
}支持 basePath 的重写
当利用 basePath 支持 进行重写时,每个 source 和 destination 会自动添加 basePath 前缀,除非你在重写中添加 basePath: false:
module.exports = {
basePath: '/docs',
async rewrites() {
return [
{
source: '/with-basePath', // 自动变成 /docs/with-basePath
destination: '/another', // 自动变成 /docs/another
},
{
// 不会将 /docs 添加到 /without-basePath,因为设置了 basePath: false
// 注意:这不能用于内部重写,例如 `destination: '/another'`
source: '/without-basePath',
destination: 'https://example.com',
basePath: false,
},
]
},
}版本历史
| 版本 | 变更 |
|---|---|
v13.3.0 | 引入 missing。 |
v10.2.0 | 引入 has。 |
v9.5.0 | 引入 Headers。 |