Troubleshooting environment variables in Gatsby and Sanity API integration
28 replies
Last updated: Aug 27, 2020
L
any pointer?
Aug 26, 2020, 6:54 AM
B
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
L
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
L
cool let me try with the prefix
Aug 26, 2020, 12:23 PM
B
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
L
unless
gatsby buildhas some sort of weird hybrid process, this is all happening at build time
Aug 26, 2020, 12:26 PM
L
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
L
it's when I am building in netlify that is breaks
Aug 26, 2020, 12:28 PM
B
Yeah, if it's working with
gatsby developit's unlikely to be the env var names.
Aug 26, 2020, 12:28 PM
L
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
L
but even then, the
<BlockConten>in my component doesn't seem to pick it up
Aug 26, 2020, 12:32 PM
L
it breaks then trying to get the image url from my _rawfield
Aug 26, 2020, 12:32 PM
L
well well well
Aug 26, 2020, 12:33 PM
L
looks like the prefix trick is working
Aug 26, 2020, 12:33 PM
B
Changing the env vars definitely didn't work?
Aug 26, 2020, 12:33 PM
B
Aha 🙂
Aug 26, 2020, 12:33 PM
L
thanks!
Aug 26, 2020, 12:34 PM
L
that's so strange
Aug 26, 2020, 12:34 PM
B
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
B
Why it worked in dev is confusing 🤷
Aug 26, 2020, 12:36 PM
B
Glad it's working for you 😄
Aug 26, 2020, 12:37 PM
L
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
L
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
L
when using the .env files you don't need the prefix
Aug 26, 2020, 12:38 PM
L
however you do when passing them straight to the script
Aug 26, 2020, 12:38 PM
L
did my head in, thanks for that!
Aug 26, 2020, 12:38 PM
B
You're welcome!
Aug 26, 2020, 12:39 PM
C
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
L
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.