Different desk structure for `sanity start` vs `sanity build`
No worries about asking again! This is definitely a common roadblock. Let me clarify what's happening with environment variables in Sanity Studio.
The good news is that SANITY_STUDIO_ prefixed variables do work in Sanity Studio, but you need to set them up correctly using environment-specific .env files.
The Solution
Create two separate .env files in your Studio folder:
.env.development (loaded during sanity start):
SANITY_STUDIO_IS_PRODUCTION=false
.env.production (loaded during sanity build):
SANITY_STUDIO_IS_PRODUCTION=true
Then in your desk structure file, access them using process.env:
// In your desk structure file
export const structure = (S) => {
const isProduction = process.env.SANITY_STUDIO_IS_PRODUCTION === 'true'
if (isProduction) {
// Production desk structure
return S.list()
.title('Content')
.items([
// your production items
])
}
// Development desk structure
return S.list()
.title('Content (Dev)')
.items([
// your dev items with extra tools, debug panels, etc.
])
}Why This Works
According to the Sanity documentation on environment variables, Sanity Studio automatically loads the appropriate .env file based on the SANITY_ACTIVE_ENV variable, which defaults to:
'development'when runningsanity start'production'when runningsanity build
The key requirements are:
- Prefix with
SANITY_STUDIO_- This is mandatory for variables to be passed to the frontend - Use
process.env- Access variables viaprocess.env.SANITY_STUDIO_YOUR_VAR(notimport.meta.env) - Create separate
.envfiles - Use.env.developmentand.env.productionfor different environments
Important Notes
- These environment variables are exposed in your built application - they're not secret. Don't put sensitive tokens here.
- Remember to add
.env*to your.gitignoreif you add any sensitive values - The values are embedded at build time, so if you change them, you need to rebuild
This approach gives you clean separation between your development and production desk structures without any hacky workarounds!
Show original thread11 replies
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.