我有一个 MERN 应用程序,其中集成了 NextJS。第一次使用 NextJS,请耐心等待。我最初在任何地方都使用过 SSR (getServerSideProps
)。
要点:
- 我有超过 150,000 个页面,其中包含永远不会更改的静态内容。
- 每周我都会添加大约 100 个新页面。
我想这里的理想情况是使用 getStaticProps
和 getStaticPaths
,并且在初始构建这 150k 页面之后,每周构建新添加的页面并保留我已有的页面已经按原样建立,因为它永远不会改变。
我怎样才能实现这个目标?我应该在这里使用revalidate
吗?我一直在文档中阅读有关它的内容,但我并不完全理解它。
I have a MERN app in which I have integrated NextJS. First time with NextJS so bear with me. I have initially used SSR everywhere (getServerSideProps
).
Key points:
- I have over 150,000 pages with static content that it's never going to change.
- Every week I add around +100 new pages.
I guess the ideal situation here would be using getStaticProps
and getStaticPaths
and, after an initial build of those 150k pages, just build the new added pages every week and keep what I already have built as it is since it's never going to change.
How can I achieve this? Should I use revalidate
here? I have been reading about it in the documentation but I don't completely understand it.
发布评论
评论(1)
这可以通过
getStaticProps
/getStaticPaths
来实现。您必须使用后备: true
或getStaticPaths
中的fallback: 'blocking'
。使用
fallback: true
,构建时未生成的路径将在第一个请求时提供后备页面,而 Next.js 静态生成页面。完成此操作后,页面将从后备页面交换到实际的完整页面。使用
fallback: 'blocking'
,构建时未生成的路径将等待 Next.js 生成 HTML,然后在完成后提供页面。与fallback: true
不同,由于没有后备,渲染会被阻止,直到生成页面,类似于服务器端渲染期间发生的情况。在这两种情况下,页面都会添加到预渲染页面列表中。对同一路径的后续请求将提供预先生成的页面。
next export
不支持这些选项,以防您依赖它。请注意,
revalidate
在getStaticProps
中用于 增量静态重新生成 - 如果您想要更新现有的生成页面。既然您提到生成的页面永远不会改变,那么就不需要使用revalidate
。That can be achieved with
getStaticProps
/getStaticPaths
. You'll have to usefallback: true
orfallback: 'blocking'
ingetStaticPaths
.With
fallback: true
the paths not generated at build time will serve a fallback page on the first request while Next.js statically generates the page. When this is done the page will be swapped from the fallback page to the actual full page.With
fallback: 'blocking'
, the paths not generated at build time will wait for the HTML to be generated by Next.js, then serve the page once that's done. Unlikefallback: true
, since there's no fallback the rendering gets blocked until the page is generated, similar to what happens during server-side rendering.In both cases the page gets added to the list of pre-rendered pages. Subsequent requests to the same path will serve the pre-generated page.
Neither of these options is supported by
next export
, in case you depend on that.Note that
revalidate
is used ingetStaticProps
for Incremental Static Regeneration - in cases where you'd want to update existing, generated pages. Since you mentioned generated pages will never change, then there's no need to userevalidate
.