Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Using system environment variables in Vercel to set preview URL for Sanity Studio.

7 replies
Last updated: Jan 26, 2022
has anyone hosting their studio on Vercel figured out a way to use its system environment variables ? i want to use the
VERCEL_URL
to set my studio's preview URL but can't find a way to add the
SANITY_STUDIO_
prefix. (i know this is more of a Vercel question, but my question there didn't get answered. hoping someone here has already figured this out!)
Jan 26, 2022, 4:05 PM
For one off env, perhaps you can do it with a npm prebuild script?

{
  "build": "sanity build",
  "prebuild": "touch .env.production && echo \"SANITY_STUDIO_VERCEL_URL=$VERCEL_URL\" >> .env.production"
}
Jan 26, 2022, 5:42 PM
Could you use a plugin to insert a custom configuration option?
Jan 26, 2022, 6:06 PM
user U
: please share how that goes! i’m considering a similar approach but using a Node script to make sure it works
cross-env
Jan 26, 2022, 8:26 PM
i got a reply from a Vercel maintainer , but neither suggested approach worked for me. (the first one just did nothing, & the second was irrelevant since it’s not a Next.js project.)
Jan 26, 2022, 8:28 PM
ok, i think i got something working! thanks
user G
, the prebuild approach seemed to do the trick. i tweaked it a bit, & did
{
  "prebuild": "cross-env NODE_ENV=production node ./scripts/prebuild.js"
}
& then that
prebuild.js
is the attached. so i’m actually making all the system variables available this way.(note that i’m on Node 14, & i had to add
"type": "module"
to my
package.json
. also, i had already added the
SANITY_ACTIVE_ENV
variable to each of my environments.)
Jan 26, 2022, 10:11 PM
i still feel like Vercel should support custom prefixes in the Environment Variables UI, but this should do the trick for now.
Jan 26, 2022, 10:14 PM
I attempted some tests on my own using sanity/node custom env values per https://www.sanity.io/docs/studio-environment-variables , hoping that vercel would have an easier time with those. I took a look at their suggestions, and didn’t have luck with that either. Hopefully this prebuild suggestion does the trick for you!
Jan 26, 2022, 11:11 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.

Was this answer helpful?