Sanity Studio 404 error on page refresh in Astro.js setup
Issue Description:I have set up Sanity Studio under the path
<http://mywebsite.com/studio|mywebsite.com/studio><http://mywebsite.com/studio/structure|mywebsite.com/studio/structure>Steps to Reproduce:1. Access the Sanity Studio at
<http://mywebsite.com/studio|mywebsite.com/studio><http://mywebsite.com/studio/structure|mywebsite.com/studio/structure><http://mywebsite.com/studio/structure|mywebsite.com/studio/structure>I understand from community discussions that one potential workaround might involve reverting to version 2.2.1 of the software. However, considering the advancements and features introduced in the latest versions, downgrading seems like an undesirable solution. Maintaining the ability to refresh pages without encountering errors seems fundamental, and I am hoping there might be an alternative solution that allows us to utilize the latest version effectively.
Could you please advise if there is another approach or fix that could resolve this issue without needing to downgrade? Your guidance on how to maintain the stability and functionality of our current setup would be greatly appreciated.
Thank you for your time and assistance.
PS: still tried to use the 2.2.1 version of @sanity/astro and got this error :
``[vite] Error when evaluating SSR module /Users/kopp/Projects/layout-ch/astro.config.mjs: |- TypeError: __vite_ssr_import_4__.default is not a function``
So this is super frustrating, barely done anything with Sanity and already stuck
:melting_face:
I understand your frustration - this is a common routing issue when embedding Sanity Studio in Astro projects. The 404 error on refresh happens because Astro needs to properly handle the Studio's client-side routing.
The Root Cause
When you embed Sanity Studio at /studio, it's a single-page application that handles its own routing client-side. When you navigate to /studio/structure, Astro initially serves the Studio correctly, but on refresh, Astro's server tries to find a file at that path and returns 404 because it doesn't exist as a static route.
The Solution
You need to configure Astro to properly serve the Studio for all /studio/* paths. Here's how:
1. Update your astro.config.mjs:
import { defineConfig } from 'astro/config';
import { sanityIntegration } from '@sanity/astro';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
integrations: [
sanityIntegration({
projectId: 'your-project-id',
dataset: 'production',
useCdn: false,
studioBasePath: '/studio'
})
],
output: 'hybrid', // or 'server' - important for Studio routing
adapter: vercel()
});The key is setting output: 'hybrid' or output: 'server' rather than output: 'static'. This ensures Astro can handle the dynamic routing the Studio needs. I can see from the community discussion that this configuration works well with @sanity/astro.
2. If deploying to Vercel:
Create or update your vercel.json in the project root:
{
"rewrites": [
{
"source": "/studio/:path*",
"destination": "/studio"
}
]
}This tells Vercel to rewrite all /studio/* requests back to /studio, allowing the Studio's client-side router to take over.
3. If deploying to Netlify:
Add to your netlify.toml:
[[redirects]]
from = "/studio/*"
to = "/studio"
status = 200About the Version Error
The __vite_ssr_import_4__.default is not a function error you encountered with v2.2.1 suggests a module import issue. This is actually a sign that downgrading isn't the right solution - you want to stay on the latest version of @sanity/astro and fix the routing configuration instead.
Additional Troubleshooting
If you're still experiencing issues after these changes:
- Clear your build cache: Delete
.astro,node_modules, and reinstall - Verify your adapter: Make sure you have the appropriate adapter installed (e.g.,
@astrojs/vercelor@astrojs/netlify) - Check your Sanity config: Ensure your
sanity.config.tsdoesn't have conflictingbasePathsettings
The Astro quickstart documentation has more details on setting up the integration properly, and you can also check the @sanity/astro GitHub repository for issues and examples.
This should resolve your refresh 404 issues without needing to downgrade. The latest version of @sanity/astro works well with proper server-side configuration!
Show original thread26 replies
Was this answer helpful?
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.