🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

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?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
DOMParser is not defined at defaultParseHtmlDec 3, 2020
Next.js and Vercel - Error! No Output Directory named "dist" foundAug 8, 2022
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
Hi all! Does anyone have problem on deoplloying on Vercel (Using Next. js as a frontend)? I have two test projects, andonce...Jan 25, 2021
Issue with compiling studio, resolved by nuking node_modules and installing with `sanity install`Aug 21, 2020
How to Disallow Robots on a SubdomainNov 12, 2020
Issue setting value of JSON field with document action in Sanity StudioApr 22, 2020
Issue with setting up Next.js E-commerce example and resolving the error related to the port.Jan 25, 2021
has anyone figured out a way to sort lists of content in the studio? i'd love a drag and drop if possibleJan 13, 2021
Error message when updating to studio version 2.2.3Jan 26, 2021

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle