resolveProductionUrl error: Cannot read properties of undefined (reading 'current')
The good news is you've already found the solution! The error happens because your Studio changes are only working locally but haven't been deployed to your hosted Sanity workspace.
The Fix: Deploy Your Studio
Run this command to push your changes to production:
sanity deployThis is exactly what solved the issue in this community thread with the same error. Local Studio changes don't automatically appear in your hosted CMS - you need to explicitly deploy them.
But There's a Problem with Your Workaround
While using document._id instead of document.slug.current stops the error, it's likely breaking your preview functionality. Your preview API routes probably expect slugs, not document IDs:
// This won't work if your API expects /about-us, not /drafts.page-abc123
`${siteUrl}/api/preview?slug=/${document._id}`The Right Solution
The original error Cannot read properties of undefined (reading 'current') means some documents don't have a slug field yet. Here's a safer approach:
export default function resolveProductionUrl(document) {
console.log('Document type:', document._type, 'Slug:', document?.slug)
const siteUrl = 'https://www.********'
// Gracefully handle missing slugs
if (!document?.slug?.current) {
console.warn('No slug found for document:', document._id)
return null // Disables preview button until slug is added
}
const slug = document.slug.current
if (document._type === 'page' || document._type === 'landingpage') {
return `${siteUrl}/api/preview?slug=/${slug}`
}
if (document._type === 'work') {
return `${siteUrl}/api/preview?slug=/work/${slug}`
}
if (document._type === 'stories') {
return `${siteUrl}/api/preview?slug=/stories/${slug}`
}
console.warn('Unrecognized document type:', document._type)
return null
}Key improvements:
- Uses optional chaining (
?.) to safely check for slug - Returns
nullwhen slug doesn't exist (preview button becomes disabled) - Uses the actual slug value your preview API expects
Complete Steps
- Update
resolveProductionUrl.jswith the safer code above - Ensure your schema has a
slugfield defined for all these document types - Run
sanity deployto push changes to production - Hard refresh your browser or clear cache
- Make sure existing documents have slugs populated (drafts without slugs will simply have no preview button)
The null return value is the correct way to handle documents without slugs - it gracefully disables the preview feature for those documents rather than throwing errors.
Show original thread4 replies
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.