Change Sanity basePath with environment variables for Netlify deployment

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
AI Update

The 404 errors you're seeing on Netlify are a common issue with Single Page Applications (SPAs) like Sanity Studio. The good news is there's a straightforward fix!

The Main Issue: Missing Redirect Rules

Sanity Studio is a SPA, which means all routing happens client-side. When you navigate to /desk or any other route, Netlify tries to find an actual file at that path and returns a 404 when it doesn't exist. You need to tell Netlify to serve your index.html for all routes.

Create a netlify.toml file in your Studio directory with this redirect rule:

[[redirects]]
  from = "/*"
  to = "/"
  status = 200

This ensures all routes are handled by your Studio's client-side router.

About the SANITY_STUDIO_BASEPATH Environment Variable

Yes, there is a reserved environment variable called SANITY_STUDIO_BASEPATH that can override the basePath setting in your Studio configuration. However, you likely don't need this unless your Studio is deployed to a subdirectory of your domain (like mysite.com/studio).

According to the official Sanity documentation, SANITY_STUDIO_BASEPATH "Sets the base path for the studio."

If you do need to use it:

  • Set it to just the path portion: /studio
  • Not the full URL like https://sanity.mysite.com/

For most Netlify deployments where the Studio is at the root of the domain, you don't need to set this variable at all.

Critical Deployment Step

If your Studio is in a subdirectory (like in a monorepo setup), make sure your netlify.toml file gets copied to your build output directory:

sanity build && cp studio/netlify.toml studio/dist

The netlify.toml file must be in your build output directory (typically dist) for Netlify to recognize it. This is a common gotcha - having the file in your source code isn't enough.

Quick Troubleshooting Checklist

  1. ✅ Create netlify.toml with the redirect rule in your Studio directory
  2. ✅ Ensure it's copied to your build output during deployment
  3. ✅ Only use SANITY_STUDIO_BASEPATH if deploying to a subdirectory path
  4. ✅ If using the variable, set it to the path only (like /studio), not a full URL

The redirect rule is the key piece that makes client-side routing work on Netlify. Once that's in place, your navigation should work smoothly!

Show original thread
20 replies
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
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
damn, i missed this in the docs
thanks!
I’ve added the redirect and https://sanity.mysite.com/ to SANITY_STUDIO_PROJECT_BASEPATH variable
Did that fix things?
That looks a bit double 🙂 And without the env variables set, you get https://sanity.mysite.dev/desk ? And this doesn’t work?
Do you have a
netlify.toml
file like Knut posted above?
netlify.toml

[[redirects]]
  from = "/*"
  to = "/"
  status = 200 
what should SANITY_STUDIO_PROJECT_BASEPATH be?
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?
no, no basePath set in project
I’ll remove the env and try again
user M
the studio is not on my root directory, but in /studio (i’m using a monorepo)
In that case you could try setting the env var to the studio path 🙂
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
the deploy script in netlify was missing … && cp studio/netlify.toml studio/distso I’m redeploying
🤞
yep, that fixed it 🎊 🌈
That makes sense but seems like a tough one to catch, so I’m glad you figured it out 🚀

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?