rewrites
重写允许您将传入的请求路径映射到不同的目标路径。
重写充当 URL 代理并隐藏目标路径,使用户看起来像没有改变他们在网站上的位置。相反,重定向将重新路由到一个新页面并显示 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是一个具有type、key和value属性的 has 对象数组。missing是一个具有type、key和value属性的 missing 对象数组。
当 rewrites 函数返回一个数组时,重写在检查文件系统(页面和 /public 文件)之后和动态路由之前应用。当 rewrites 函数返回一个具有特定形状的数组对象时,从 Next.js v10.1 开始,可以改变这种行为并进行更精细的控制。
module.exports = {
async rewrites() {
return {
beforeFiles: [
// These rewrites are checked after headers/redirects
// and before all files including _next/public files which
// allows overriding page files
{
source: '/some-page',
destination: '/somewhere-else',
has: [{ type: 'query', key: 'overrideMe' }],
},
],
afterFiles: [
// These rewrites are checked after pages/public files
// are checked but before dynamic routes
{
source: '/non-existent',
destination: '/somewhere-else',
},
],
fallback: [
// These rewrites are checked after both pages/public files
// and dynamic routes are checked
{
source: '/:path*',
destination: `https://my-old-site.com/:path*`,
},
],
}
},
}须知:
beforeFiles中的重写在匹配源后不会立即检查文件系统/动态路由,它们会持续检查所有beforeFiles。
Next.js 路由的检查顺序是:
- headers 被检查/应用
- redirects 被检查/应用
- 代理
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', // The :path parameter isn't used here so will be automatically passed in the query
},
]
},
}如果目标中使用了参数,则不会自动在查询中传递任何参数。
module.exports = {
async rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*', // The :path parameter is used here so will not be automatically passed in the query
},
]
},
}如果参数已在目标中使用,您仍然可以通过在 destination 中指定查询来手动在查询中传递参数。
module.exports = {
async rewrites() {
return [
{
source: '/:first/:second',
destination: '/:first?second=:second',
// Since the :first parameter is used in the destination the :second parameter
// will not automatically be added in the query although we can manually add it
// as shown above
},
]
},
}路径匹配
允许路径匹配,例如 /blog/:slug 将匹配 /blog/hello-world(无嵌套路径)
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug',
destination: '/news/:slug', // Matched parameters can be used in the destination
},
]
},
}通配符路径匹配
要匹配通配符路径,您可以在参数后使用 *,例如 /blog/:slug* 将匹配 /blog/a/b/c/d/hello-world
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*', // Matched parameters can be used in the destination
},
]
},
}正则表达式路径匹配
要匹配正则表达式路径,您可以在参数后将正则表达式括在括号中,例如 /blog/:slug(\\d{1,}) 将匹配 /blog/123 但不匹配 /blog/abc
module.exports = {
async rewrites() {
return [
{
source: '/old-blog/:post(\\d{1,})',
destination: '/blog/:post', // Matched parameters can be used in the destination
},
]
},
}以下字符 (, ), {, }, [, ], |, \, ^, ., :, *, +, -, ?, $ 用于正则表达式路径匹配,因此当在 source 中用作非特殊值时,它们必须通过在其前面添加 \\ 进行转义。
module.exports = {
async rewrites() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
},
]
},
}标头、Cookie 和查询匹配
要仅在 header、cookie 或查询值也匹配 has 字段或不匹配 missing 字段时才匹配重写,可以使用 has 或 missing 字段。source 和所有 has 项都必须匹配,并且所有 missing 项都不能匹配,重写才会应用。
has 和 missing 项可以具有以下字段
type:String- 必须是header、cookie、host或query。key:String- 要匹配的所选类型的键。value:String或undefined- 要检查的值,如果未定义,则任何值都将匹配。可以使用类似正则表达式的字符串来捕获值的特定部分,例如,如果first-(?<paramName>.*)用于first-second,那么second将在目标中与:paramName一起使用。
module.exports = {
async rewrites() {
return [
// if the header `x-rewrite-me` is present,
// this rewrite will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// if the header `x-rewrite-me` is not present,
// this rewrite will be applied
{
source: '/:path*',
missing: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// if the source, query, and cookie are matched,
// this rewrite will be applied
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// the page value will not be available in the
// destination since value is provided and doesn't
// use a named capture group e.g. (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
destination: '/:path*/home',
},
// if the header `x-authorized` is present and
// contains a matching value, this rewrite will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
destination: '/home?authorized=:authorized',
},
// if the host is `example.com`,
// this rewrite will be applied
{
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', // Matched parameters can be used in the destination
},
]
},
}如果您正在使用 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', // automatically becomes /docs/with-basePath
destination: '/another', // automatically becomes /docs/another
},
{
// does not add /docs to /without-basePath since basePath: false is set
// Note: this can not be used for internal rewrites e.g. `destination: '/another'`
source: '/without-basePath',
destination: 'https://example.com',
basePath: false,
},
]
},
}版本历史
| 版本 | 更改 |
|---|---|
v13.3.0 | missing 已添加。 |
v10.2.0 | has 已添加。 |
v9.5.0 | 标头已添加。 |
这有帮助吗?