Next.js SSR/ISR 为非静态页面创建 HTML 静态文件

Next.js SSR/ISR creating HTML static file for non-static page

什么会导致使用 Next.js 为非静态页面创建静态 HTML?我不确定它是否会导致任何问题,但我注意到在解决此特定页面的问题时发生了这种情况。这是预期的吗?

我的意思是我们有一个页面,我们称之为“page1”,它没有 getStaticPaths()getStaticProps() 功能。

此页面确实依赖于服务器端数据,但未使用 getInitialProps()getServerSideProps(),因为它们已被弃用。

当我 cd 进入构建目录时,我可以看到 .next/server/pages/page1.html - 这是预期的吗?没有 page1.json 文件。如果不是,什么会导致创建此 HTML 文件?

是的,这是预期的。 Next.js' Automatic Static Optimization 正在播放。

如果您的页面不包含 getServerSideProps/getInitialProps,则 Next.js 将自动静态优化页面并将其预呈现为静态 HTML.

来自文档:

If getServerSideProps or getInitialProps is present in a page, Next.js will switch to render the page on-demand, per-request (meaning Server-Side Rendering).

If the above is not the case, Next.js will statically optimize your page automatically by prerendering the page to static HTML.

During prerendering, the router's query object will be empty since we do not have query information to provide during this phase. After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.

next build will emit .html files for statically optimized pages.