如何使用 Next.js 自动静态站点优化并仍然为 Netlify 导出?

cam*_*nch 5 static-site netlify next.js

我一直致力于实现Next 9.3 的一些更新。我一直在移动getInitialPropsgetServerSideProps并注意到我exportPathMap对这些页面变得动态变得不满意。运行一切正常next,但是当我去运行时next build && next export,我遇到了一些问题。

在文档的静态HTML导出它指出If your pages don't have getInitialProps you may not need next export at all; next build is already enough thanks to Automatic Static Optimization.我能得到那个工作愉快地与我的新getServerSideProps电话,当我运行next build && next start。我需要采取哪些步骤才能使其也适用,next export以便我可以通过 Netlify 进行部署。这是我尝试运行时遇到的错误示例next export

Error occurred prerendering page "/videos/[videos_title]". Read more: https://err.sh/next.js/prerender-error:
Error: Error for page /videos/[videos_title]: pages with `getServerSideProps` can not be exported. See more info here: https://err.sh/next.js/gssp-export
Run Code Online (Sandbox Code Playgroud)

Gan*_*esh 5

使用 SSR 构建的应用程序不能部署在 Netlify 或任何其他静态托管站点(除了支持 NextJS SSR 部署的 Vercel)

当您选择 SSR(使用 getServerSideProps)时,使用该命令毫无意义,next export因为它会尝试创建与 SSR 完全相反的静态内容。

   

  • 一种部署方式是通过创建具有正确路由配置的自定义 server.js 文件在虚拟服务器(如 EC2)中运行它。
  • 另一种简单快捷的方法是使用Vercel (以前的 Zeit)来部署 SSR 实现的应用程序,在那里他们可以明智地处理它

Vercel 的 SSR 应用程序部署文档很差。幸运的是,我从支持团队那里获得了以下信息,并要求他们更新文档以详细说明 Vercel 中的 SSR 部署。

在 Vercel 中部署时,

  • 将构建命令作为next buildnpm run build
  • 将输出目录保留为

注意:带有自定义 server.js 的应用程序将无法在 Vercel 中正常工作,在这种情况下,请使用虚拟服务器(如 EC2)