resolveProductionUrl error: Cannot read properties of undefined (reading 'current')

4 replies
Last updated: Oct 6, 2023
Hello,I need help with a an existing project that I have taken over. We have suddenly encountered this error, when ever our content creaters are trying to create new content on the CMS we are getting the following error:.


`An error was thrown while trying to get your preview url: An error occurred while resolving
resolveProductionUrl
from Production: Cannot read properties of undefined (reading 'current')`

We have updated to sanity v3, and I made the following changes to resolveProductionUrl.js changed document.slug.current to document._id and also changed the apiVersion: ‘2023-06-27’,

This has resolved the error in my local sanity environment but its still there in the CMS. Please help.


export default function resolveProductionUrl(document) {
  console.log(document._type)

  const siteUrl = '<https://www>.********'

  if (document._type == 'page' || document._type == 'landingpage') {
    const url = `${siteUrl}/api/preview?slug=/${document._id}`
    console.log('Preview URL:', url)
    return url
  }

  if (document._type == 'work') {
    const url = `${siteUrl}/api/preview?slug=/work/${document._id}`
    console.log('Preview URL:', url)
    return url
  }

  if (document._type == 'stories') {
    const url = `${siteUrl}/api/preview?slug=/stories/${document._id}`
    console.log('Preview URL:', url)
    return url
  }
  console.error('Unrecognized document type:', document._type)
}
and sanity.config.js looks like this


import { defineConfig, definePlugin } from "sanity";
import { deskTool } from "sanity/desk";
import schemas from "./schemas/schema";
import resolveProductionUrl from './resolveProductionUrl'

const sharedConfig = definePlugin({
  plugins: [deskTool()],
  schema: {
    types: schemas,
  }
});

export default defineConfig([
  {
    name: "Production",
    title: "Production",
    projectId: "******",
    dataset: "production",
    basePath: "/prod",
    plugins: [sharedConfig()],
    document: {
      productionUrl: async (prev, context) => {
        return resolveProductionUrl(context.document);
      },
    },
  },
  {
    name: "staging",
    title: "Staging",
    projectId: "*******",
    dataset: "development",
    basePath: "/staging",
    plugins: [sharedConfig()],
  },
]);
AI Update

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 deploy

This 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 null when slug doesn't exist (preview button becomes disabled)
  • Uses the actual slug value your preview API expects

Complete Steps

  1. Update resolveProductionUrl.js with the safer code above
  2. Ensure your schema has a slug field defined for all these document types
  3. Run sanity deploy to push changes to production
  4. Hard refresh your browser or clear cache
  5. 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 thread
4 replies
Have you deployed your changes?
Yes, turns out we forgot to run sanity deploy.
works fine now.
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?