服务器端渲染不适用于生产构建 Next.js [英] Server side rendering not work on production build Next.js

查看:40
本文介绍了服务器端渲染不适用于生产构建 Next.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to deploy my Next.js app to my own hosting. I run next export to build the app, it works and i able to run it on my hosting server. But the problem is the app not server side rendering (reloading the app on any page except main page will cause error):

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

I'm new on Next.js and my previous apps on react can't do server side rendering, so i a bit confuse about it. Any help will be welcome, Thank You!

推荐答案

根据文档,next export 用于将您的项目导出为静态 HTML.更重要的是,文档状态(重点是我的):

According to the documentation, next export is for exporting your project into static HTML. More importantly, the docs state (emphasis mine):

next export 适用于您的网页都没有的情况服务器端或增量数据要求(虽然静态渲染的页面仍然可以获取数据在客户端就好了).

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).

如果您想制作一个只有部分页面的混合网站预渲染为静态 HTML,Next.js 已经自动完成了为你!阅读自动静态优化了解详情.

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 也会导致增量静态生成等功能和再生被禁用,因为它们需要下次启动或无服务器部署才能发挥作用.

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 服务器运行,或者 通过 Next.js(next build && next start),或使用 自定义节点服务器.

This means that if your routes need server-side data fetching with getServerSideProps, you'll need to do run the project as a Node.js server either through Next.js (next build && next start), or with a custom node server.

这篇关于服务器端渲染不适用于生产构建 Next.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆