Migrating resolveProductionUrl from Sanity v2 to v3
I'll help you migrate your production preview URL from Sanity Studio v2 to v3. The migration docs can be confusing, but the good news is that the concept is similar - it just needs to be configured in a different location.
The Migration
In Studio v3, the resolveProductionUrl functionality has been replaced by the productionUrl property in your sanity.config.ts (or .js) file. Here's how to migrate your v2 code:
Your v2 code:
// resolveProductionUrl.js
export default function resolveProductionUrl(document) {
if (document._type == "page"){
return `https://sitename.netlify.app/page-preview/${document._id}`
}
if (document._type == "product"){
return `https://sitename.netlify.app/product-preview/${document._id}`
}
if (document._type == "post"){
return `https://sitename.netlify.app/post-preview/${document._id}`
}
}Your v3 code:
// sanity.config.ts (or .js)
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
export default defineConfig({
projectId: '<your-project-id>',
dataset: 'production',
plugins: [structureTool()],
schema: {
types: schemaTypes,
},
document: {
productionUrl: async (prev, context) => {
const {document} = context
if (document._type === "page") {
return `https://sitename.netlify.app/page-preview/${document._id}`
}
if (document._type === "product") {
return `https://sitename.netlify.app/product-preview/${document._id}`
}
if (document._type === "post") {
return `https://sitename.netlify.app/post-preview/${document._id}`
}
return prev
}
}
})Key Differences
- Location: It's now configured in the
documentproperty of your main config, not a separate file - Function signature: The function receives
(prev, context)where the document is available ascontext.document - Return fallback: You should return
previf none of your conditions match - Can be async: The function can be async if you need to do any asynchronous operations
Alternative: Using document slug
If you're using slugs instead of IDs for your preview URLs, you can access them like this:
productionUrl: async (prev, context) => {
const {document} = context
const slug = document?.slug?.current
if (!slug) return prev
if (document._type === "page") {
return `https://sitename.netlify.app/page-preview/${slug}`
}
// ... rest of your conditions
}The preview button should now appear in your document actions bar when editing documents of these types. You can read more about configuring productionUrl in the Studio configuration docs.
Hope this helps with your v3 upgrade!
Show original thread6 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.