Discussion on using environment variables in Sanity Studio for different environments
11 replies
Last updated: May 28, 2020
K
Apologize for asking the same question again, but this is a bit of a roadblock for me at the moment...I'm trying to render my desk structure differently depending on whether Sanity Studio is being run via
sanity startor has been built with
sanity build.I've tried using environment variables, but it doesn't seem like any of the environment variables get passed through to the front end even if I prepend them with
SANITY_STUDIO_.If anyone can help, it would be greatly appreciated.
May 27, 2020, 8:05 PM
T
Have you created the files .
env.development&
.env.production?
May 27, 2020, 8:11 PM
T
or how do you create the variables?
May 27, 2020, 8:11 PM
M
Indeed, what Hafffe said - you would need these two specific file names (not just
.env) to have the vars get picked up at
sanity start(
.env.development) or `sanity build`/`sanity deploy` (
.env.production). The prefix you’re using looks correct!
May 28, 2020, 11:16 AM
P
Indeed, what Hafffe said - you would need these two specific file names (not just
.env) to have the vars get picked up at
sanity start(
.env.development) or `sanity build`/`sanity deploy` (
.env.production). The prefix you’re using looks correct!
May 28, 2020, 11:16 AM
M
Okay, I will give that a shot.So does that mean
this bit of documentation isn't accurate?
this bit of documentation isn't accurate?
The environment is defined by the value of theYou're saying I'll actually need to create aSANITY_ACTIVE_ENVenvironment variable. If not defined, it will default todevelopmentwhen running thesanity startcommand, while it will useproductionwhen runningsanity buildandsanity deploy.
.env.productionwith something like
SANITY_STUDIO_ENVIRONMENT="production"and a
.env.developmentcontaining
SANITY_STUDIO_ENVIRONMENT="development"...? There isn't an automatic way to determine within code if you're in dev or prod?
May 28, 2020, 2:44 PM
P
Ah, no, both
(To test, try without the latter
SANITY_ACTIVE_ENVand
NODE_ENVshould be available without needing those
.env.*files. How are you checking for them? Normally you’d use something like this:
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV || 'development'
|| 'development'part)
May 28, 2020, 3:13 PM
M
Yeah, that is exactly what I'm currently doing. It's returning
undefined.
May 28, 2020, 3:56 PM
M
And I'm doing that in a desk structure implementation file, to be clear.
May 28, 2020, 3:57 PM
P
Hmm that’s strange indeed - just tried to reproduce. When I add this to the top of `deskStructure.js`:
I get
Are you getting
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV console.log('env:',env)
env: developmentlogged to console in a development environment (
sanity start).
Are you getting
undefinedfor the exact same approach?
May 28, 2020, 4:00 PM
M
Lemme try one tweak...
May 28, 2020, 4:04 PM
M
Okay...Sorry about that – got caught up in some other things.Looks like
NODE_ENVdoes work. It's
SANITY_ACTIVE_ENVthat is
undefined. So that gives me something usable. 👍
May 28, 2020, 5:27 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.