Changing basePath for Sanity Studio on Netlify using environmental variables
20 replies
Last updated: Apr 22, 2020
V
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
M
There’s a way! If you set
More info here:
https://www.sanity.io/docs/studio-environment-variables#reserved-variable-names-cc239c4841f0
SANITY_STUDIO_PROJECT_BASEPATHin your environment variables, this should override whatever path you’ve set in your
sanity.jsonfile.
More info here:
https://www.sanity.io/docs/studio-environment-variables#reserved-variable-names-cc239c4841f0
Apr 20, 2020, 4:07 PM
V
damn, i missed this in the docs
Apr 20, 2020, 4:11 PM
V
thanks!
Apr 20, 2020, 4:11 PM
V
I’ve added the redirect and https://sanity.mysite.com/ to SANITY_STUDIO_PROJECT_BASEPATH variable
Apr 20, 2020, 4:13 PM
M
Did that fix things?
Apr 20, 2020, 4:15 PM
V
no. now I get https://sanity.mysite.dev/https:/sanity.mysite.dev/desk 😆
Apr 20, 2020, 4:24 PM
M
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
M
Do you have a
netlify.tomlfile like Knut posted above?
Apr 20, 2020, 4:27 PM
V
netlify.toml
[[redirects]] from = "/*" to = "/" status = 200
Apr 20, 2020, 4:28 PM
V
what should SANITY_STUDIO_PROJECT_BASEPATH be?
Apr 20, 2020, 4:28 PM
M
Unless the Studio is not in your root directory, you should be fine without the env var and a
netlify.tomlfile just like the one you posted. There’s no basepath set under
projectin your
sanity.jsonfile, correct?
Apr 20, 2020, 4:30 PM
V
no, no basePath set in project
Apr 20, 2020, 4:31 PM
V
I’ll remove the env and try again
Apr 20, 2020, 4:31 PM
V
user M
the studio is not on my root directory, but in /studio (i’m using a monorepo)Apr 20, 2020, 4:35 PM
M
In that case you could try setting the env var to the studio path 🙂
Apr 20, 2020, 4:36 PM
C
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
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
C
the deploy script in netlify was missing … && cp studio/netlify.toml studio/distso I’m redeploying
🤞
🤞
Apr 22, 2020, 5:11 PM
C
yep, that fixed it 🎊 🌈
Apr 22, 2020, 5:14 PM
P
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.