Troubleshooting environment variable issues with Gatsby and Sanity APIs

28 replies
Last updated: Aug 27, 2020
any pointer?
Aug 26, 2020, 6:54 AM
user L
- are you accessing the Sanity APIs at run-time (react in the browser), or build time? When Gatsby builds your app, it will only share environment variables that start with
GATSBY_
to the React app. Try changing your variables to
GATSBY_SANITYXXX
and rebuilding. Interestingly, I would imagine it wouldn't work in dev too though 🤔
Aug 26, 2020, 12:21 PM
that's when I'm building, it works greats in dev mode because I have
.env.xx
files with my envs that are picked up in the gatsby-config file
Aug 26, 2020, 12:23 PM
cool let me try with the prefix
Aug 26, 2020, 12:23 PM
I wonder if you're pulling in the data at build-time, but React is generating the images at run-time, which is where it can then not get the env vars. Hope this works for you 🤞
Aug 26, 2020, 12:24 PM
unless
gatsby build
has some sort of weird hybrid process, this is all happening at build time
Aug 26, 2020, 12:26 PM
and it's definitely an issue of env vars because building manually when I have the
.env.xx
files works
Aug 26, 2020, 12:27 PM
it's when I am building in netlify that is breaks
Aug 26, 2020, 12:28 PM
Yeah, if it's working with
gatsby develop
it's unlikely to be the env var names.
Aug 26, 2020, 12:28 PM
it's just really strange, if I run my build with
export MYVAR=var npm run build
I have
console.log(process.env)
in my gastby-node.js and I get the full list of env vars
Aug 26, 2020, 12:32 PM
but even then, the
<BlockConten>
in my component doesn't seem to pick it up
Aug 26, 2020, 12:32 PM
it breaks then trying to get the image url from my _rawfield
Aug 26, 2020, 12:32 PM
well well well
Aug 26, 2020, 12:33 PM
looks like the prefix trick is working
Aug 26, 2020, 12:33 PM
Changing the env vars definitely didn't work?
Aug 26, 2020, 12:33 PM
Aha 🙂
Aug 26, 2020, 12:33 PM
thanks!
Aug 26, 2020, 12:34 PM
that's so strange
Aug 26, 2020, 12:34 PM
So, what I think is happening is:
• Gatsby builds, and pulls the Sanity data in
• The data is stored in
public/page-data/...
as JSON• When you render the page, it's react running in the browser - react uses the JSON to render the content
• You then pass the content into the
<BlockContent>
component, which pulls out the image data from the API, requiring the
projectid
and
dataset
againThis last point is where it needs the
GATSBY_
prefixed variables.
Aug 26, 2020, 12:36 PM
Why it worked in dev is confusing 🤷
Aug 26, 2020, 12:36 PM
Glad it's working for you 😄
Aug 26, 2020, 12:37 PM
yeah right, I think it worked in dev ( and build manually on my machine) because I was using the .env files
Aug 26, 2020, 12:37 PM
when building with Netlify you can't have the .env files so you have to pass the variables to the netlify building script
Aug 26, 2020, 12:38 PM
when using the .env files you don't need the prefix
Aug 26, 2020, 12:38 PM
however you do when passing them straight to the script
Aug 26, 2020, 12:38 PM
did my head in, thanks for that!
Aug 26, 2020, 12:38 PM
You're welcome!
Aug 26, 2020, 12:39 PM
also you could use
gatsby-plugin-env-variables


    {
      resolve: 'gatsby-plugin-env-variables',
      options: {
        allowList: [
          'SANITY_PROJECT_ID',
          'SANITY_DATASET',
          'SANITY_PROJECT_DATASET',
          'SANITY_PROJECT_NAME',
        ]
      },
    },
what abouyt using
Aug 26, 2020, 7:18 PM
nice one
user C
I'll have a look at that thanks
Aug 27, 2020, 12:30 AM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in