【问题标题】:How can I deploy a Next.js App to Netlify?如何将 Next.js 应用程序部署到 Netlify?
【发布时间】:2020-12-06 14:28:56
【问题描述】:

我的 Next.js 应用部署目前遇到问题。在遵循 netlify 博客中关于如何使用 netlify for Next.js 创建服务器端呈现应用程序的指南后,我能够成功构建,但我的网站似乎无法正常工作。

该网站目前位于https://sleepy-murdock-8d5427.netlify.app/。错误信息将粘贴在下方。

{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "FetchError: request to http://localhost:3000/api/notes failed, reason: connect ECONNREFUSED 127.0.0.1:3000",
"trace": [
"Runtime.UnhandledPromiseRejection: FetchError: request to http://localhost:3000/api/notes failed, reason: connect ECONNREFUSED 127.0.0.1:3000",
"    at process.<anonymous> (/var/runtime/index.js:35:15)",
"    at process.emit (events.js:315:20)",
"    at processPromiseRejections (internal/process/promises.js:209:33)",
"    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
]
}

【问题讨论】:

标签: next.js netlify netlify-function


【解决方案1】:

Netlify 只能提供静态网页。 出于这个原因,我不建议在 netlify 上提供 ssr 应用程序。 -> 它会将您的 ssr 应用程序创建为静态网页。因此,大多数 next.js 函数(如 getStaticPropsgetStaticPaths)都无法工作。 为此,您需要有 函数

要在 netlify 上提供服务端渲染应用,您必须构建和导出每个页面。

为此,我将这些行添加到我的 package.json 文件中。

{
  "scripts": {
    "build: "next build",
    "export": "next export",
    "deploy": "npm run build && npm run export"
  }
}

当您将其部署到 netlify 时,您的网站应该是活动的并呈现为静态应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 2022-07-09
    • 2022-10-18
    • 2019-01-02
    • 2020-12-07
    • 2021-10-01
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多