How to create a testing environment in Sanity without affecting the live application.
Good news - datasets in Sanity are completely isolated from each other, so your cloned testing dataset won't affect your production app at all! Here's what you need to know to keep everything safe:
Why Your Production App is Safe
As explained in the datasets documentation, each dataset is a completely isolated collection of documents and assets. Data in one dataset cannot be directly queried from another dataset, so your testing dataset is inherently separate from production.
Best Practices to Ensure Complete Isolation
1. Configure your frontend to use the correct dataset
Make sure your live application is explicitly configured to only query the production dataset. In your client configuration, set the dataset name:
const client = createClient({
projectId: 'your-project-id',
dataset: 'production', // Explicitly set this
apiVersion: '2024-01-01',
useCdn: true
})2. Use environment variables for different environments
The recommended approach from the multi-environment deployments guide is to use environment variables. For example:
- Production:
NEXT_PUBLIC_SANITY_DATASET=production - Testing:
NEXT_PUBLIC_SANITY_DATASET=testing
This ensures your live app always points to the correct dataset.
3. Configure Studio for the right dataset
If you're running separate Studio instances, configure each one in sanity.config.ts:
export default defineConfig({
projectId: 'your-project-id',
dataset: process.env.SANITY_STUDIO_API_DATASET || 'production',
// ... other config
})You can use .env.development and .env.production files to automatically switch datasets based on whether you're running sanity start (development) or sanity deploy (production).
4. Be careful with webhooks and Sanity Functions
If you have webhooks or Sanity Functions set up, make sure they're configured to only trigger on your production dataset. When setting up webhooks, you can filter by dataset using GROQ:
// Only trigger on production dataset
sanity::dataset() == "production"In Sanity Functions, you can check the dataset in your event handler:
export default async function(event) {
if (event.dataset !== 'production') {
return; // Ignore non-production events
}
// Your production logic
}5. Manage permissions appropriately
Consider setting up different access levels for your datasets through project settings. This ensures team members testing in the cloned dataset have appropriate permissions without accidentally affecting production.
The Bottom Line
As long as your live application's client configuration explicitly points to your production dataset (which it should already be doing), there's zero risk of your testing dataset affecting the live app. The datasets are completely separate at the infrastructure level. Your team can freely experiment, break things, and test in the cloned dataset with complete confidence!
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.