How to create a testing environment in Sanity without affecting the live application.

7 replies
Last updated: Jun 29, 2023
Hi, everyone!
My team is new to Sanity, and are taking over the project from our previous developers. We are building new features to our app, and are copying our current dataset in order to create a testing environment for new functionality. Since they are new to Sanity, they are worried that a new dataset for testing, will ruin or disturb the current live application, that is supposed to go on as is while we build and test all new functions.
So the question is, how do we make sure that the current live app is not affected by testing and developing in the cloned dataset?
AI Update

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!

export your data from your dataset, and import it to a new dataset, nothing in the live app should change
Thank you for the reply! That is what my understanding was as well, but their concern apparently is this: "maybe you are not aware of Sanity(Desk), single piece of wrong code can crash the sanity desk. That is the main reason I don't want to touch the live project" And they want to set up a whole new, cloned, project. Is this necessary? Is this an actual possibility, that it will crash the whole project?
Well as long as you dont deploy the changes it shouldn’t matter to the editors
Okay, so what they are trying to do is basically making a test environment to build and test new functionality, and this functionality is to be used in a fully redeveloped (new) app, that is currently under testing. There will be new functionality added to the Sanity studio, that will not apply to the current live application, only to the new one. Are there any issues to be aware of here, or will it not matter to the current app, as long as nothing is actively deployed to that app?In short: Can we keep one dataset going for the live application, while having the other dataset with added functionalities to an app which is under testing?
I suppose we would need to actively change which dataset the studio is working from, every time we need to do editing in the current app?
yup the schema is completely separate from the dataset
basically your dataset is only stored json documents
Okay, superb! Thanks for helping 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?