【问题标题】:Next.js custom _error page only works locallyNext.js 自定义 _error 页面仅在本地工作
【发布时间】:2021-09-01 03:22:55
【问题描述】:

这里有一个链接到我的仓库来演示这个问题:https://github.com/jonathanwelton/nextjs-custom-error-test

我正在尝试在 Next.js 中使用自定义 _error 页面,当您访问 /throw(引发错误的页面)时,该页面适用于本地生产构建(npm run build && npm start),但 Netlify 都没有或 Vercel 在部署应用程序时尊重它。我刚刚收到一个堆栈跟踪,你可以在这里看到:https://nextjs-custom-error-test.netlify.app/throw

恢复到 Next.js 9.4.4,自定义 _error 页面可在 Vercel 上运行,但您似乎无法在 Netlify 上部署低于版本 10 的 Next.js 应用。

我刚刚更新了应用程序以使用 Next v11.0.0,但问题仍然存在。

谁能解释我做错了什么?

【问题讨论】:

标签: next.js netlify vercel


【解决方案1】:

如文档中所述:https://nextjs.org/docs/advanced-features/custom-error-page

我们需要传递props,判断是显示实际页面还是错误页面。

像这样修改代码

export async function getServerSideProps() {
  try {
     // Your statements
     throw new Error('a new error')

     return {
      props: { data: [] },
     }
  } catch(e) {
     return {
         props: { errorCode: 500 },
     }
  }
}

const Throw = ({ errorCode, ...data }) {
  if (errorCode) {
    return <Error statusCode={errorCode} />
  }

  return <div>{...}</div>
}
export default Throw;

【讨论】:

  • 嘿@p2pdops,我更新了 throw 页面以匹配您上面的更改,但它仍然存在相同的问题:nextjs-custom-error-test.netlify.app/throw 值得重申的是旧代码确实适用于 Next.js 9.4.4
  • 嗨@JonathanWelton,它说访问reactjs.org/docs/… 所以React 正在尝试渲染一个对象.. 这意味着_error.js 已发送但有一些对象正在通过做结帐
猜你喜欢
  • 2014-04-23
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2022-11-17
  • 2022-01-05
相关资源
最近更新 更多