reactjs - NextJS + Contentful => getInitialProps 中的 getEntries 请求破坏了生产构建

标签 reactjs next.js contentful vercel

堆栈:NextJS、内容丰富+现在

概述:在 getInitialProps 异步函数中调用 contentful 的 getEntries() 请求。在本地环境中,一切工作正常,我收到帖子,然后成功地将它们作为 Prop 传递到我可以渲染它们的页面。

问题:当我尝试使用 Now 进行部署时,出现此错误:

Error: The resource could not be found. at notFoundError (/zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7731:19) at /zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7854:13 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Function.module.exports.HB77.Post.getInitialProps (/zeit/31b54c53/.next/serverless/pages/p/[id].js:1198:15) at async loadGetInitialProps (/zeit/31b54c53/.next/serverless/pages/p/[id].js:3451:17) { sys: { type: 'Error', id: 'NotFound' }, details: { type: 'Entry', id: 'undefined', environment: 'master', space: undefined } } Error occurred prerendering page "/p/[id]" https://err.sh/zeit/next.js/prerender-error: Error: Failed to render serverless page at Object._default [as default] (/zeit/31b54c53/node_modules/next/dist/export/worker.js:12:212) at processTicksAndRejections (internal/process/task_queues.js:93:5)

代码:

const Post = props => {
  ...
}

Post.getInitialProps = async function({ query }) {

  const contentfulClient = contentful.createClient({
    accessToken: `${process.env.ACCESS_TOKEN}`,
    space: `${process.env.SPACE}`
  });

  const res = await contentfulClient.getEntry(`${query.id}`);

  return {
    post: res
  };
};

export default Post;

最佳答案

看起来您的 ENV 可能缺少:

details: { type: 'Entry', id: 'undefined', environment: 'master', space: undefined } 

process.env.space未定义

关于reactjs - NextJS + Contentful => getInitialProps 中的 getEntries 请求破坏了生产构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59725254/

相关文章:

javascript - Object.Assign 不创建新实例

reactjs - 如何使用 'withRouter' 和 'withTranslation' 导出 react 组件

node.js - 将contentful导入到react-native中

reactjs - 在 Next.js 中使用 WebSocket

javascript - Gatsby:类型错误:无法读取 null 的属性 'map'

graphql - 在 Gatsby 和 Contentful 中按日期过滤

reactjs - 无法修复未处理的拒绝错误-

javascript - 如何通过将函数作为 Prop 传递给其子级来更新父级的状态

reactjs - 如何在元素 className 中执行三元运算

javascript - 部署 Next.js