getInitialProps中的NextJS + Contentful => getEntries请求中断了生产构建

堆栈:NextJS,内容丰富且立即提供

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

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

错误:找不到资源。 在notFoundError(/zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7731:19) 在/zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7854:13 在processTicksAndRejections(内部/流程/task_queues.js:93:5) 在异步Function.module.exports.HB77.Post.getInitialProps(/zeit/31b54c53/.next/serverless/pages/p/[id].js:1198:15) 在异步loadGetInitialProps(/zeit/31b54c53/.next/serverless/pages/p/[id].js:3451:17){ sys:{类型:“错误”,id:“未找到”}, 细节: { 类型:“条目”, id:“未定义”, 环境:“主人”, 空间:未定义 } } 在呈现页面“ / p / [id]” https://err.sh/zeit/next.js/prerender-error时发生错误:错误:无法呈现无服务器页面 在Object._default [作为默认值](/zeit/31b54c53/node_modules/next/dist/export/worker.js:12:212) 在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;
plmoknnm 回答:getInitialProps中的NextJS + Contentful => getEntries请求中断了生产构建

您的ENV似乎不足:

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

process.env.spaceundefined

,

我将机密存储在.env,.env.build中,并通过其CLI将其添加到Now机密中。为了向生产版本公开这些秘密,我创建了next.config.js文件,在其中创建了如下所示的模块导出:


    module.exports = {
      env: {
        SPACE: process.env.SPACE,}
    };

我的问题是我不小心将next.config.js文件添加到gitignore中,这导致env机密返回为未定义。 ?

本文链接:https://www.f2er.com/2774805.html

大家都在问