Upgrading to v3 and struggling with production previews, but successfully resolved with help from community.

6 replies
Last updated: Jan 30, 2023
Hi All. I'm upgrading a studio to v3 and it's all working apart from the production previews. In v2 I was using (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} ``
}

}
But I'm struggling to get it working now. I find the migration docs difficult to understand in this area. Could anyone point me in the right direction? Thanks in advance!
Jan 16, 2023, 1:29 PM
Okay, to be more precise, in the migration docs it has this example:

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
    },
  },
})
How can I modify this if I just want to query by document._id rather than slug?
Jan 16, 2023, 3:23 PM
Something like this should work:
document: {
    // prev is the result from previous plugins and can be composed
    productionUrl: async (prev, {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}>`
        }
  }

      return prev
    },
  },
})
Jan 17, 2023, 9:05 PM
Thanks so much for the reply. I can see you have a tidal wave of support queries to deal with! πŸ™
Jan 17, 2023, 9:53 PM
This is all working now. I left out the:
 return prev
Fully upgraded to v3 now, thanks again for the support!
Jan 23, 2023, 12:24 PM
Congrats on finishing your migration!
Jan 30, 2023, 6:55 PM
Thanks! I'm impressed with V3. Everything is so fast now. Onwards! πŸš€
Jan 30, 2023, 6:58 PM

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?