Unlock seamless workflows and faster delivery with our latest releases – get the details

Changing basePath for Sanity Studio on Netlify using environmental variables

20 replies
Last updated: Apr 22, 2020
is there a way to change the basePath for sanity with environmental variables? Deploying my studio to Netlify is giving me 404 whenever I navigate anywhere in the Studio
Apr 20, 2020, 3:54 PM
It sounds like you're missing a redirect rule? Since the studio is a SPA all sub paths needs to redirect to the index.html file. Check here for a configuration file https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/netlify.toml
Apr 20, 2020, 4:07 PM
There’s a way! If you set
SANITY_STUDIO_PROJECT_BASEPATH
in your environment variables, this should override whatever path you’ve set in your
sanity.json
file.
More info here:
https://www.sanity.io/docs/studio-environment-variables#reserved-variable-names-cc239c4841f0
Apr 20, 2020, 4:07 PM
damn, i missed this in the docs
Apr 20, 2020, 4:11 PM
thanks!
Apr 20, 2020, 4:11 PM
I’ve added the redirect and https://sanity.mysite.com/ to SANITY_STUDIO_PROJECT_BASEPATH variable
Apr 20, 2020, 4:13 PM
Did that fix things?
Apr 20, 2020, 4:15 PM
Apr 20, 2020, 4:24 PM
That looks a bit double 🙂 And without the env variables set, you get https://sanity.mysite.dev/desk ? And this doesn’t work?
Apr 20, 2020, 4:26 PM
Do you have a
netlify.toml
file like Knut posted above?
Apr 20, 2020, 4:27 PM
netlify.toml

[[redirects]]
  from = "/*"
  to = "/"
  status = 200 
Apr 20, 2020, 4:28 PM
what should SANITY_STUDIO_PROJECT_BASEPATH be?
Apr 20, 2020, 4:28 PM
Unless the Studio is not in your root directory, you should be fine without the env var and a
netlify.toml
file just like the one you posted. There’s no basepath set under
project
in your
sanity.json
file, correct?
Apr 20, 2020, 4:30 PM
no, no basePath set in project
Apr 20, 2020, 4:31 PM
I’ll remove the env and try again
Apr 20, 2020, 4:31 PM
user M
the studio is not on my root directory, but in /studio (i’m using a monorepo)
Apr 20, 2020, 4:35 PM
In that case you could try setting the env var to the studio path 🙂
Apr 20, 2020, 4:36 PM
I removed the env variable and i get this strange behaviour (my site is a copy of the sanity portfolio starter)
going to my netlify url
https://sanity.mysite.dev redirects to https://sanity.mysite.dev/desk and shows the studio
going straight to
https://sanity.mysite.dev/desk gives me 404
Apr 22, 2020, 5:08 PM
the deploy script in netlify was missing … && cp studio/netlify.toml studio/distso I’m redeploying
🤞
Apr 22, 2020, 5:11 PM
yep, that fixed it 🎊 🌈
Apr 22, 2020, 5:14 PM
That makes sense but seems like a tough one to catch, so I’m glad you figured it out 🚀
Apr 22, 2020, 6:42 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?