🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Error when generating link to production environment for preview in Studio v3

2 replies
Last updated: Jun 9, 2023
Hi everyoneIm trying to generate a link to a production environment for preview in studio v3. Im using this guide
https://www.sanity.io/docs/preview-content-on-site .
But I keep getting the error:

error was thrown while trying to get your preview url: An error occurred while resolving `resolveProductionUrl`: client.fetch is not a function. (In ‘client.fetch(
*`*[_id == $postId][0].slug.current`,*
{postId: document._id}
)’, ‘client.fetch’ is undefined
My code looks like this:
productionUrl: async (prev, context) => {
      // context includes the client and other details
      const {client, dataset, document} = context

      if (document._type.includes('page')) {
        const slug = await client.fetch(
          `*[_id == $postId][0].slug.current`,
          {postId: document._id}
        )

        console.log(document._id)
        const params = new URLSearchParams()
        params.set('preview', 'true')
        params.set('dataset', dataset)

        return `<https://my-domain.com/${slug}?${params}>`
      }

      return prev
    },

Jun 7, 2023, 7:24 AM
I think the bit of docs is outdated! Can you try:
const {getClient, dataset, document} = context

const client = getClient({ apiVersion: '2023-06-27' })
Jun 7, 2023, 5:20 PM
works like a charm : ) Thanks a lot!
Jun 9, 2023, 8:42 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?