Sanity Studio 404 error on /desk route when hosting on Netlify

3 replies
Last updated: Nov 17, 2021
This might be a noob question, but we are struggling with 404 when hosting Sanity on Netlify.
When visiting the site it will redirect to
/desk
. For example in Safari you’ll get the cookie not accepted dialog and after clicking all of the login and ok buttons we’ll end up on a Page Not Found page. The same can happen in other browsers as well, but it’s possible to “fix” it by going to the root of the hosted url (deleting /desk from the path)
Do we need to define some redirects or something here?
AI Update

This is a common issue when deploying Sanity Studio to Netlify! The problem is that Sanity Studio is a single-page application (SPA), and when you navigate to routes like /desk, Netlify tries to find an actual file at that path instead of serving your index.html file.

The solution is to add a _redirects file to tell Netlify to serve your index.html for all routes. Here's how to fix it:

The Solution

Create a file called _redirects (no file extension) in your Studio's public folder with this content:

/*    /index.html   200

This tells Netlify to serve your index.html file for all routes (the /* wildcard) with a 200 status code, which allows the Studio's client-side routing to handle the navigation properly.

Where to Place the File

The location depends on your setup:

  • Standard Studio setup: Create public/_redirects in your Studio directory
  • Monorepo setup: Create it in studio/public/_redirects (or wherever your Studio lives)

The public folder's contents get copied to the build output when you run sanity build, so the _redirects file will end up in your dist folder where Netlify can read it.

Netlify Build Settings

Make sure your Netlify configuration is correct:

  • Build command: sanity build (or npm run build if you've set up a build script)
  • Publish directory: dist (or studio/dist for monorepos)

Why This Happens

Single-page applications like Sanity Studio use client-side routing, meaning routes like /desk don't correspond to actual files on the server. Without the redirect rule, Netlify looks for a file called desk and returns a 404 when it doesn't find one. The _redirects file tells Netlify to always serve the index.html, which then loads the Studio JavaScript that handles the routing.

After deploying with this change, navigating directly to /desk or any other Studio route should work correctly in all browsers!

Show original thread
3 replies
Hi Jørn - not a noob question!
This is because it's missing a single-page app routing configuration - if the file/path requested cannot be found, it should fallback to the root (
index.html
).
A
netlify.toml
like the following usually does the trick:

[[redirects]]
  from = "/*"
  to = "/"
  status = 200
That did indeed solve the problem. Thank you!
Great 🙂

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?