Troubleshooting environment variable issues with Gatsby and Sanity APIs
28 replies
Last updated: Aug 27, 2020
D
any pointer?
Aug 26, 2020, 6:54 AM
D
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_SANITYXXXand rebuilding. Interestingly, I would imagine it wouldn't work in dev too though 🤔
Aug 26, 2020, 12:21 PM
J
that's when I'm building, it works greats in dev mode because I have
.env.xxfiles with my envs that are picked up in the gatsby-config file
Aug 26, 2020, 12:23 PM
A
cool let me try with the prefix
Aug 26, 2020, 12:23 PM
D
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
A
unless
gatsby buildhas some sort of weird hybrid process, this is all happening at build time
Aug 26, 2020, 12:26 PM
J
and it's definitely an issue of env vars because building manually when I have the
.env.xxfiles works
Aug 26, 2020, 12:27 PM
A
it's when I am building in netlify that is breaks
Aug 26, 2020, 12:28 PM
D
Yeah, if it's working with
gatsby developit's unlikely to be the env var names.
Aug 26, 2020, 12:28 PM
A
it's just really strange, if I run my build with
export MYVAR=var npm run buildI 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
D
but even then, the
<BlockConten>in my component doesn't seem to pick it up
Aug 26, 2020, 12:32 PM
D
it breaks then trying to get the image url from my _rawfield
Aug 26, 2020, 12:32 PM
D
well well well
Aug 26, 2020, 12:33 PM
D
looks like the prefix trick is working
Aug 26, 2020, 12:33 PM
D
Changing the env vars definitely didn't work?
Aug 26, 2020, 12:33 PM
D
Aha 🙂
Aug 26, 2020, 12:33 PM
J
thanks!
Aug 26, 2020, 12:34 PM
D
that's so strange
Aug 26, 2020, 12:34 PM
J
So, what I think is happening is:
• Gatsby builds, and pulls the Sanity data in
• The data is stored in
• You then pass the content into the
• 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
projectidand
datasetagainThis last point is where it needs the
GATSBY_prefixed variables.
Aug 26, 2020, 12:36 PM
D
Why it worked in dev is confusing 🤷
Aug 26, 2020, 12:36 PM
D
Glad it's working for you 😄
Aug 26, 2020, 12:37 PM
J
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
D
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
D
when using the .env files you don't need the prefix
Aug 26, 2020, 12:38 PM
D
however you do when passing them straight to the script
Aug 26, 2020, 12:38 PM
D
did my head in, thanks for that!
Aug 26, 2020, 12:38 PM
D
You're welcome!
Aug 26, 2020, 12:39 PM
D
also you could use
what abouyt using
gatsby-plugin-env-variables
{ resolve: 'gatsby-plugin-env-variables', options: { allowList: [ 'SANITY_PROJECT_ID', 'SANITY_DATASET', 'SANITY_PROJECT_DATASET', 'SANITY_PROJECT_NAME', ] }, },
Aug 26, 2020, 7:18 PM
D
nice one
user C
I'll have a look at that thanksAug 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.