
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeBased on the Sanity documentation I found, here's what you need to know about customizing the Sanity Studio's HTML and Vite configuration:
Sanity Studio v3+ uses Vite as its build tool, and you can extend the built-in Vite configuration through the Sanity CLI config file.
To customize the Vite configuration and add script tags to index.html, create or modify sanity.cli.js (or sanity.cli.ts) in your Studio project root:
// sanity.cli.js
import {defineCliConfig} from 'sanity/cli'
export default defineCliConfig({
api: {
// your API config...
},
vite: {
// Your Vite configuration overrides go here
plugins: [
// Add your custom Vite plugins here
],
},
})For your specific use case with import-meta-env for runtime environment variables while self-hosting, you can use Vite's transformIndexHtml hook:
// sanity.cli.js
import {defineCliConfig} from 'sanity/cli'
export default defineCliConfig({
api: {
projectId: 'your-project-id',
dataset: 'your-dataset',
},
vite: {
plugins: [
{
name: 'inject-runtime-env',
transformIndexHtml(html) {
return html.replace(
'</head>',
`<script src="/env.js"></script></head>`
)
},
},
],
},
})Alternatively, if you're using the @import-meta-env/unplugin package directly:
// sanity.cli.js
import {defineCliConfig} from 'sanity/cli'
import importMetaEnv from '@import-meta-env/unplugin'
export default defineCliConfig({
api: {
projectId: 'your-project-id',
dataset: 'your-dataset',
},
vite: {
plugins: [
importMetaEnv.vite({
example: '.env.example',
env: '.env',
}),
],
},
})If your custom Vite plugin with transformIndexHtml isn't running, it's likely because:
sanity.cli.js, not sanity.config.js (which is for Studio configuration, not build configuration)After adding your Vite configuration:
npm run dev or npx sanity devnpm run build or npx sanity builddist/index.html to verify your script tag was injectedWhile Sanity Studio does support a static directory for assets, you cannot directly modify the index.html template. The Vite plugin approach with transformIndexHtml is the correct way to inject custom script tags.
The key insight is that Sanity Studio uses Vite under the hood, so any standard Vite plugin approach should work when properly configured in sanity.cli.js. You can learn more about Vite configuration options in their documentation.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store