1

When I try to connect to Contentful with Gatsby, I get this error message :

Starting to fetch data from Contentful
info Fetching default locale

 ERROR 

Accessing your Contentful space failed.
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.

Used options:
spaceId: "*******ed"
accessToken: "*******ed"
host (default value): "cdn.contentful.com"
environment (default value): "master"
downloadLocal (default value): false
localeFilter (default value): [Function]
forceFullSync (default value): false
pageLimit (default value): 100
useNameForId (default value): true

not finished source and transform nodes - 0.320s

My code in gatsby-config.js :

module.exports = {

  siteMetadata: {
    title: `Gatsby`,
    siteUrl: `http://localhost8000`,
    author: '****',
  },
  plugins: [
    {
      resolve: `gatsby-source-contentful`,
      options: {
        spaceId: ${`process.env.CONTENTFUL_SPACE_ID`},
        accessToken: ${`process.env.CONTENTFUL_ACCESS_TOKEN`},
        // My code only works with this syntax ${`...`}
      }
    },
  ]
}

I saw Brent Arias's response on this page: https://stackoverflow.com/questions/60892938/ Accessing your Contentful space failed with gatsby-source-contentful. He overwrites and creates the contentful space, and locally relaunches the starter script "rg-portfolio" which natively contains a contentful configuration. It also refers to a "contentful-data.json" file in the starter.

I use the "hello world" starter, so I added the gatsby-source-contentful plugin myself, and it didn't create a "contentful-data.json" file, and I don't see anything about this json file on the plugin's documentation. I deleted and recreated my contentful space, then I reinstalled the plugin, but it still doesn't work ...

Any idea?

Flo
  • 17
  • 6
  • Can you share the Contentful part in your gatsby-config.js? :) – stefan judis May 01 '20 at 18:00
  • 1
    Your `gatsby-config` should look like [this](https://gist.github.com/antibland/128e20647acd5dec07adeaf85916f86c) – Andy Hoffman May 05 '20 at 06:10
  • @AndyHoffman Yes I tried to write the code this way, but I don't know why it didn't work. I had an error message because it did not recognize it as a string. – Flo May 06 '20 at 16:28

1 Answers1

1

I noticed an issue in how you're doing string literals.

Change this:

spaceId: ${`process.env.CONTENTFUL_SPACE_ID`},
accessToken: ${`process.env.CONTENTFUL_ACCESS_TOKEN`},

Into either this (recommended):

spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,

Or this:

spaceId: `${process.env.CONTENTFUL_SPACE_ID}`,
accessToken: `${process.env.CONTENTFUL_ACCESS_TOKEN}`,
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61