路由片段配置
如果
dynamicIO
标志处于打开状态,则此页面上概述的选项将被禁用,并且最终将在将来被弃用。¥The options outlined on this page are disabled if the
dynamicIO
flag is on, and will eventually be deprecated in the future.
路由段选项允许你通过直接导出以下变量来配置 页面、布局 或 路由处理程序 的行为:
¥The Route Segment options allows you to configure the behavior of a Page, Layout, or Route Handler by directly exporting the following variables:
选项 | 类型 | 默认 |
---|---|---|
experimental_ppr | boolean | |
dynamic | 'auto' | 'force-dynamic' | 'error' | 'force-static' | 'auto' |
dynamicParams | boolean | true |
revalidate | false | 0 | number | false |
fetchCache | 'auto' | 'default-cache' | 'only-cache' | 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store' | 'auto' |
runtime | 'nodejs' | 'edge' | 'nodejs' |
preferredRegion | 'auto' | 'global' | 'home' | string | string[] | 'auto' |
maxDuration | number | 由部署平台设置 |
选项
¥Options
experimental_ppr
为布局或页面启用 部分预渲染 (PPR)。
¥Enable Partial Prerendering (PPR) for a layout or page.
export const experimental_ppr = true
// true | false
export const experimental_ppr = true
// true | false
dynamic
将布局或页面的动态行为更改为完全静态或完全动态。
¥Change the dynamic behavior of a layout or page to fully static or fully dynamic.
export const dynamic = 'auto'
// 'auto' | 'force-dynamic' | 'error' | 'force-static'
export const dynamic = 'auto'
// 'auto' | 'force-dynamic' | 'error' | 'force-static'
很高兴知道:
app
目录中的新模型支持fetch
请求级别的细粒度缓存控制,而不是pages
目录中页面级别的getServerSideProps
和getStaticProps
的二进制全有或全无模型。dynamic
选项是一种选择返回到以前的模型的方法,以方便起见并提供更简单的迁移路径。¥Good to know: The new model in the
app
directory favors granular caching control at thefetch
request level over the binary all-or-nothing model ofgetServerSideProps
andgetStaticProps
at the page-level in thepages
directory. Thedynamic
option is a way to opt back in to the previous model as a convenience and provides a simpler migration path.
-
'auto'
(默认):默认选项会尽可能多地缓存,而不阻止任何组件选择动态行为。¥**
'auto'
** (default): The default option to cache as much as possible without preventing any components from opting into dynamic behavior. -
'force-dynamic'
:强制 动态渲染,这将导致在请求时为每个用户渲染路由。该选项相当于:¥**
'force-dynamic'
**: Force dynamic rendering, which will result in routes being rendered for each user at request time. This option is equivalent to:-
getServerSideProps()
在pages
目录中。¥
getServerSideProps()
in thepages
directory. -
将布局或页面中每个
fetch()
请求的选项设置为{ cache: 'no-store', next: { revalidate: 0 } }
。¥Setting the option of every
fetch()
request in a layout or page to{ cache: 'no-store', next: { revalidate: 0 } }
. -
将段配置设置为
export const fetchCache = 'force-no-store'
¥Setting the segment config to
export const fetchCache = 'force-no-store'
-
-
'error'
:如果任何组件使用 动态 API 或未缓存的数据,则会引发错误,从而强制静态渲染并缓存布局或页面的数据。该选项相当于:¥**
'error'
**: Force static rendering and cache the data of a layout or page by causing an error if any components use Dynamic APIs or uncached data. This option is equivalent to:-
getStaticProps()
在pages
目录中。¥
getStaticProps()
in thepages
directory. -
将布局或页面中每个
fetch()
请求的选项设置为{ cache: 'force-cache' }
。¥Setting the option of every
fetch()
request in a layout or page to{ cache: 'force-cache' }
. -
将段配置设置为
fetchCache = 'only-cache', dynamicParams = false
。¥Setting the segment config to
fetchCache = 'only-cache', dynamicParams = false
. -
dynamic = 'error'
将dynamicParams
的默认值从true
更改为false
。你可以通过手动设置dynamicParams = true
选择重新动态渲染不是由generateStaticParams
生成的动态参数的页面。¥
dynamic = 'error'
changes the default ofdynamicParams
fromtrue
tofalse
. You can opt back into dynamically rendering pages for dynamic params not generated bygenerateStaticParams
by manually settingdynamicParams = true
.
-
-
'force-static'
:通过强制cookies
、headers()
和useSearchParams()
返回空值来强制静态渲染并缓存布局或页面的数据。¥**
'force-static'
**: Force static rendering and cache the data of a layout or page by forcingcookies
,headers()
anduseSearchParams()
to return empty values.
很高兴知道:
¥Good to know:
如何迁移 从
getServerSideProps
和getStaticProps
到dynamic: 'force-dynamic'
和dynamic: 'error'
的说明可以在 升级指南 中找到。¥Instructions on how to migrate from
getServerSideProps
andgetStaticProps
todynamic: 'force-dynamic'
anddynamic: 'error'
can be found in the upgrade guide.
dynamicParams
控制当访问不是使用 generateStaticParams 生成的动态段时会发生什么情况。
¥Control what happens when a dynamic segment is visited that was not generated with generateStaticParams.
export const dynamicParams = true // true | false,
export const dynamicParams = true // true | false,
-
true
(默认):generateStaticParams
中未包含的动态段是按需生成的。¥**
true
** (default): Dynamic segments not included ingenerateStaticParams
are generated on demand. -
false
:未包含在generateStaticParams
中的动态段将返回 404。¥**
false
**: Dynamic segments not included ingenerateStaticParams
will return a 404.
很高兴知道:
¥Good to know:
该选项替换
pages
目录中getStaticPaths
的fallback: true | false | blocking
选项。¥This option replaces the
fallback: true | false | blocking
option ofgetStaticPaths
in thepages
directory.要在第一次访问时静态渲染所有路径,你需要在
generateStaticParams
中返回一个空数组或利用export const dynamic = 'force-static'
。¥To statically render all paths the first time they're visited, you'll need to return an empty array in
generateStaticParams
or utilizeexport const dynamic = 'force-static'
.当
dynamicParams = true
时,该段使用 流式服务器渲染。¥When
dynamicParams = true
, the segment uses Streaming Server Rendering.如果使用了
dynamic = 'error'
和dynamic = 'force-static'
,则会将默认的dynamicParams
更改为false
。¥If the
dynamic = 'error'
anddynamic = 'force-static'
are used, it'll change the default ofdynamicParams
tofalse
.
revalidate
设置布局或页面的默认重新验证时间。此选项不会覆盖各个 fetch
请求设置的 revalidate
值。
¥Set the default revalidation time for a layout or page. This option does not override the revalidate
value set by individual fetch
requests.
export const revalidate = false
// false | 0 | number
export const revalidate = false
// false | 0 | number
-
false
(默认):默认启发式缓存任何将cache
选项设置为'force-cache'
或在使用 动态 API 之前发现的fetch
请求。语义上相当于revalidate: Infinity
,这实际上意味着资源应该无限期地缓存。个别fetch
请求仍然可以使用cache: 'no-store'
或revalidate: 0
来避免被缓存并使路由动态渲染。或者将revalidate
设置为低于路由默认值的正数,以增加路由的重新验证频率。¥**
false
** (default): The default heuristic to cache anyfetch
requests that set theircache
option to'force-cache'
or are discovered before a Dynamic API is used. Semantically equivalent torevalidate: Infinity
which effectively means the resource should be cached indefinitely. It is still possible for individualfetch
requests to usecache: 'no-store'
orrevalidate: 0
to avoid being cached and make the route dynamically rendered. Or setrevalidate
to a positive number lower than the route default to increase the revalidation frequency of a route. -
0
:确保布局或页面始终是 动态渲染,即使未发现动态 API 或未缓存的数据提取。此选项更改未将cache
选项设置为'no-store'
的fetch
请求的默认值,但保留选择进入'force-cache'
或按原样使用正revalidate
的fetch
请求。¥**
0
**: Ensure a layout or page is always dynamically rendered even if no Dynamic APIs or uncached data fetches are discovered. This option changes the default offetch
requests that do not set acache
option to'no-store'
but leavesfetch
requests that opt into'force-cache'
or use a positiverevalidate
as is. -
number
:(很快)将布局或页面的默认重新验证频率设置为n
秒。¥**
number
**: (in seconds) Set the default revalidation frequency of a layout or page ton
seconds.
很高兴知道:
¥Good to know:
重新验证值需要可静态分析。例如
revalidate = 600
有效,但revalidate = 60 * 10
无效。¥The revalidate value needs to be statically analyzable. For example
revalidate = 600
is valid, butrevalidate = 60 * 10
is not.使用
runtime = 'edge'
时,重新验证值不可用。¥The revalidate value is not available when using
runtime = 'edge'
.在开发中,页面始终按需渲染,并且从不缓存。这允许你立即看到更改,而无需等待重新验证期过去。
¥In Development, Pages are always rendered on-demand and are never cached. This allows you to see changes immediately without waiting for a revalidation period to pass.
重新验证频率
¥Revalidation Frequency
-
单个路由的每个布局和页面的最低
revalidate
将决定整个路由的重新验证频率。这可确保子页面与其父布局一样频繁地重新验证。¥The lowest
revalidate
across each layout and page of a single route will determine the revalidation frequency of the entire route. This ensures that child pages are revalidated as frequently as their parent layouts. -
各个
fetch
请求可以设置比路由默认revalidate
更低的revalidate
,以增加整个路由的重新验证频率。这使你可以根据某些条件动态选择对某些路由进行更频繁的重新验证。¥Individual
fetch
requests can set a lowerrevalidate
than the route's defaultrevalidate
to increase the revalidation frequency of the entire route. This allows you to dynamically opt-in to more frequent revalidation for certain routes based on some criteria.
fetchCache
This is an advanced option that should only be used if you specifically need to override the default behavior.
默认情况下,Next.js 会缓存在使用任何 动态 API 之前可访问的任何 fetch()
请求,并且不会缓存使用动态 API 后发现的 fetch
请求。
¥By default, Next.js will cache any fetch()
requests that are reachable before any Dynamic APIs are used and will not cache fetch
requests that are discovered after Dynamic APIs are used.
fetchCache
允许你覆盖布局或页面中所有 fetch
请求的默认 cache
选项。
¥fetchCache
allows you to override the default cache
option of all fetch
requests in a layout or page.
export const fetchCache = 'auto'
// 'auto' | 'default-cache' | 'only-cache'
// 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store'
export const fetchCache = 'auto'
// 'auto' | 'default-cache' | 'only-cache'
// 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store'
-
'auto'
(默认):默认选项是使用它们提供的cache
选项在动态 API 之前缓存fetch
请求,而不是在动态 API 之后缓存fetch
请求。¥**
'auto'
** (default): The default option to cachefetch
requests before Dynamic APIs with thecache
option they provide and not cachefetch
requests after Dynamic APIs. -
'default-cache'
:允许将任何cache
选项传递给fetch
,但如果未提供任何选项,则将cache
选项设置为'force-cache'
。这意味着即使在动态 API 之后的fetch
请求也被视为静态的。¥**
'default-cache'
**: Allow anycache
option to be passed tofetch
but if no option is provided then set thecache
option to'force-cache'
. This means that evenfetch
requests after Dynamic APIs are considered static. -
'only-cache'
:如果未提供选项,则通过将默认值更改为cache: 'force-cache'
来确保所有fetch
请求选择缓存,如果任何fetch
请求使用cache: 'no-store'
,则会导致错误。¥**
'only-cache'
**: Ensure allfetch
requests opt into caching by changing the default tocache: 'force-cache'
if no option is provided and causing an error if anyfetch
requests usecache: 'no-store'
. -
'force-cache'
:通过将所有fetch
请求的cache
选项设置为'force-cache'
,确保所有fetch
请求选择缓存。¥**
'force-cache'
**: Ensure allfetch
requests opt into caching by setting thecache
option of allfetch
requests to'force-cache'
. -
'default-no-store'
:允许将任何cache
选项传递给fetch
,但如果未提供任何选项,则将cache
选项设置为'no-store'
。这意味着即使在动态 API 之前的fetch
请求也被视为动态的。¥**
'default-no-store'
**: Allow anycache
option to be passed tofetch
but if no option is provided then set thecache
option to'no-store'
. This means that evenfetch
requests before Dynamic APIs are considered dynamic. -
'only-no-store'
:如果未提供选项,则通过将默认值更改为cache: 'no-store'
来确保所有fetch
请求选择退出缓存,如果任何fetch
请求使用cache: 'force-cache'
,则会导致错误¥**
'only-no-store'
**: Ensure allfetch
requests opt out of caching by changing the default tocache: 'no-store'
if no option is provided and causing an error if anyfetch
requests usecache: 'force-cache'
-
'force-no-store'
:通过将所有fetch
请求的cache
选项设置为'no-store'
,确保所有fetch
请求选择退出缓存。这会强制每个请求重新获取所有fetch
请求,即使它们提供了'force-cache'
选项。¥**
'force-no-store'
**: Ensure allfetch
requests opt out of caching by setting thecache
option of allfetch
requests to'no-store'
. This forces allfetch
requests to be re-fetched every request even if they provide a'force-cache'
option.
跨路由路段行为
¥Cross-route segment behavior
-
跨单个路由的每个布局和页面设置的任何选项都需要彼此兼容。
¥Any options set across each layout and page of a single route need to be compatible with each other.
-
如果
'only-cache'
和'force-cache'
都提供,则'force-cache'
获胜。如果同时提供了'only-no-store'
和'force-no-store'
,则'force-no-store'
获胜。强制选项会更改整个路由的行为,因此带有'force-*'
的单个线段将防止'only-*'
引起的任何错误。¥If both the
'only-cache'
and'force-cache'
are provided, then'force-cache'
wins. If both'only-no-store'
and'force-no-store'
are provided, then'force-no-store'
wins. The force option changes the behavior across the route so a single segment with'force-*'
would prevent any errors caused by'only-*'
. -
'only-*'
和'force-*'
选项的目的是保证整个路由是完全静态或完全动态的。这意味着:¥The intention of the
'only-*'
and'force-*'
options is to guarantee the whole route is either fully static or fully dynamic. This means:-
不允许在一条路由中组合使用
'only-cache'
和'only-no-store'
。¥A combination of
'only-cache'
and'only-no-store'
in a single route is not allowed. -
不允许在一条路由中组合使用
'force-cache'
和'force-no-store'
。¥A combination of
'force-cache'
and'force-no-store'
in a single route is not allowed.
-
-
如果子级提供
'auto'
或'*-cache'
,则父级无法提供'default-no-store'
,因为这可能会使相同的提取具有不同的行为。¥A parent cannot provide
'default-no-store'
if a child provides'auto'
or'*-cache'
since that could make the same fetch have different behavior.
-
-
通常建议将共享父布局保留为
'auto'
并自定义子段分歧的选项。¥It is generally recommended to leave shared parent layouts as
'auto'
and customize the options where child segments diverge.
runtime
我们建议使用 Node.js 运行时来渲染应用,并使用 Edge 运行时来渲染中间件(仅受支持的选项)。
¥We recommend using the Node.js runtime for rendering your application, and the Edge runtime for Middleware (only supported option).
export const runtime = 'nodejs'
// 'nodejs' | 'edge'
export const runtime = 'nodejs'
// 'nodejs' | 'edge'
-
'nodejs'
(默认)¥**
'nodejs'
** (default) -
'edge'
了解有关 不同的运行时间 的更多信息。
¥Learn more about the different runtimes.
preferredRegion
export const preferredRegion = 'auto'
// 'auto' | 'global' | 'home' | ['iad1', 'sfo1']
export const preferredRegion = 'auto'
// 'auto' | 'global' | 'home' | ['iad1', 'sfo1']
对 preferredRegion
的支持以及支持的区域取决于你的部署平台。
¥Support for preferredRegion
, and regions supported, is dependent on your deployment platform.
很高兴知道:
¥Good to know:
如果未指定
preferredRegion
,它将继承最近的父布局的选项。¥If a
preferredRegion
is not specified, it will inherit the option of the nearest parent layout.根布局默认为
all
区域。¥The root layout defaults to
all
regions.
maxDuration
默认情况下,Next.js 不限制服务器端逻辑的执行(渲染页面或处理 API)。部署平台可以使用 Next.js 构建输出中的 maxDuration
来添加特定的执行限制。例如,Vercel。
¥By default, Next.js does not limit the execution of server-side logic (rendering a page or handling an API).
Deployment platforms can use maxDuration
from the Next.js build output to add specific execution limits.
For example, on Vercel.
注意:此设置需要 Next.js 13.4.10
或更高版本。
¥Note: This settings requires Next.js 13.4.10
or higher.
export const maxDuration = 5
export const maxDuration = 5
很高兴知道:
¥Good to know:
如果使用 服务器操作,请在页面级别设置
maxDuration
以更改页面上使用的所有服务器操作的默认超时。¥If using Server Actions, set the
maxDuration
at the page level to change the default timeout of all Server Actions used on the page.
generateStaticParams
generateStaticParams
函数可以与 动态路由段 结合使用来定义将在构建时静态生成的路由段参数列表,而不是在请求时按需生成。
¥The generateStaticParams
function can be used in combination with dynamic route segments to define the list of route segment parameters that will be statically generated at build time instead of on-demand at request time.
有关详细信息,请参阅 API 参考。
¥See the API reference for more details.
版本历史
¥Version History
版本 | |
---|---|
v15.0.0-RC | export const runtime = "experimental-edge" 已弃用。codemod 可用。 |