Sanity Studio login screen source context error on dev and production

10 replies
Last updated: Mar 1, 2024
Hi, we are stuck on this source context error on the login screen of Sanity studio. This trace is from localhost when running dev, but we get the same when building for production, but obfuscated. So maybe someone has an idea from this trace?
AI Update

The "Could not find source context" error on the Sanity Studio login screen is typically a dependency or build issue. Based on the GitHub discussion, here are the most common solutions:

Quick Fixes

1. Clean install your dependencies (most common fix):

rm -rf node_modules
rm package-lock.json  # or yarn.lock / pnpm-lock.yaml
npm install

This resolves most cases where the error appears during development or build. Something often gets out of sync during upgrades, especially when migrating between major versions.

2. Pin your Sanity package versions: Remove carets from your package.json to ensure exact version matching:

{
  "dependencies": {
    "sanity": "3.x.x",  // instead of "^3.x.x"
    "@sanity/vision": "3.x.x"
  }
}

3. Update @sanity/vision: If you're using the Vision plugin, make sure it's on the latest version that matches your Studio version. Version mismatches here can cause context errors.

What's Happening

This error occurs when a component tries to use Sanity's internal React context (like useClient, useDataset, or useSource hooks) but can't find the context provider. This usually means:

  • Mismatched package versions between sanity core and plugins
  • Duplicate React instances in your node_modules
  • Corrupted dependency tree from a partial upgrade

The error shows up at login because that's when Studio components first try to access the Sanity client configuration through React context.

If You're Migrating from v2 to v3

Make sure all your @sanity/* packages are on v3 versions. Mixed v2/v3 packages will definitely cause this error. Check your package.json for any stragglers.

After cleaning dependencies, restart your dev server completely - sometimes the build cache needs clearing too. If you're still stuck after trying these steps, sharing your package.json dependencies would help diagnose further!

Show original thread
10 replies
What version of the Studio are you running?
Sanity 3.30.0
We are still stuck on this one. Looking for any guidance on how to move forward.
Which browser is this happening in? Are you using a VPN or firewall?
All browsers, chrome, edge, safari mac and iPhone. No VPN or changes to firewalls.
The users that have logged out are unable to load the site at all. So I have informed everyone else NOT to log out. Running an old v2 on localhost allows my self to log in. And then I can use the current version again. So, it's some logged out part of the site that crashes.
I tried setting up a new blank studio with the same id. That works, at least on my machine and with no schema or settings. So now starts the huge project of adding some πŸ§€ πŸ• πŸ₯§ one by one to see where it breaks.
Still: I would think the fact that it works once I get past the login should be a clue that one of you
πŸ§™β€β™‚οΈ gets aπŸ’‘by..

πŸ™
Turned out to be that useDataset from sanity, which was used to choose the logo, was not happy being called on the login screen. πŸ™‡β€β™‚οΈ Doh!
Thanks for updating us!
Are there any up to date tutorials on how to detect dataset and environment that does not have these types of pitfalls? Especially current dataset and the stage/prod environment.

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?