2

Stack: NextJS, Contentful + Now

Overview: Invoked contentful's getEntries() request within getInitialProps async function. In local environment, everything is working perfectly, I receive the posts then successfully pass them as props to the page where I can render them.

Issue: When I try to deploy with Now, I am getting this error:

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)

Code:

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;
Tyler Knapp
  • 93
  • 1
  • 9

2 Answers2

2

looks like your ENVs could be lacking:

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

The process.env.space is undefined

Edrian
  • 608
  • 4
  • 14
0

I stored my secrets within an .env, .env.build as well as added them to Now secrets through their CLI. To expose the secrets to the production build, I created a next.config.js file where I created a modules export like the following:


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

My problem was that I accidentally added the next.config.js file to the gitignore which caused the env secrets to be returned as undefined.

Tyler Knapp
  • 93
  • 1
  • 9