Unlock seamless workflows and faster delivery with our latest releases – get the details

Migrating Production URL Resolver

The resolveProductionUrl-function comes in handy when you want to preview your content in its production environment. In v2 you would install the production-preview part to get access to this functionality. In the new Config API it comes ready to use out of the box!

The top-level document.resolveProductionUrl property accepts an async callback function which is invoked with an array of any previous values as the first argument and the config context as the second.

This is what it would look like in v2:

// sanity.json
{
  "root": true,
  "project": {
    "name": "My Project"
  },
  // ...
  "parts": [
    {
      "implements": "part:@sanity/production-preview/resolve-production-url",
      "path": "./resolveProductionUrl.js"
    }
  ]
}

// ./resolveProductionUrl.js
export default function resolveProductionUrl(document) {
  if (document._type === 'post') {
    return `https://my-site.com/posts/${document.slug.current}`
  }

  return null
}

And here is what it would look like in v3:

// sanity.config.ts
import {defineConfig} from 'sanity'
import {deskTool} from 'sanity/desk'


export default defineConfig({
  name: 'default',
  title: 'My Cool Project',
  projectId: 'my-project-id',
  dataset: 'production',
  document: {
    // prev is the result from previous plugins and can be composed
    productionUrl: async (prev, context) => {
      // context includes the client an other details
      const {client, dataset, document} = context

      if (document._type === 'post') {
        // you can now use async/await 🎉
        const slug = await client.fetch(
          `*[_type == 'routeInfo' && post._ref == $postId][0].slug.current`,
          {postId: document._id}
        )

        const params = new URLSearchParams()
        params.set('preview', 'true')
        params.set('dataset', dataset)

        return `https://my-site.com/posts/${slug}?${params}`
      }

      return prev
    },
  },
})

Feedback or questions?

These guides are under development. If you have questions or suggestions for improvement, then we want to hear from you. Get in touch via our GitHub discussions page.

Was this article helpful?