服务器端渲染不适用于生产版本 Next.js

Server side rendering not work on production build Next.js

我想将我的 Next.js 应用程序部署到我自己的主机上。我 运行 next export 构建应用程序,它可以工作,并且我能够 运行 在我的托管服务器上。但问题是应用程序不是服务器端渲染(在主页面以外的任何页面重新加载应用程序都会导致错误):

我是 Next.js 的新手,我以前的 React 应用程序不能进行服务器端渲染,所以我对此有点困惑。欢迎任何帮助,谢谢!

根据文档,next export 用于将您的项目导出为静态 HTML。更重要的是,the docs state(强调我的):

next export is intended for scenarios where none of your pages have server-side or incremental data requirements (though statically-rendered pages can still fetch data on the client side just fine).

If you're looking to make a hybrid site where only some pages are prerendered to static HTML, Next.js already does that automatically for you! Read up on Automatic Static Optimization for details.

next export also causes features like Incremental Static Generation and Regeneration to be disabled, as they require next start or a serverless deployment to function.

这意味着如果您的路由需要使用 getServerSideProps 获取服务器端数据,您需要将项目 运行 作为 Node.js 服务器 through Next.js (next build && next start), or with a custom node server.