Upgrading to v3 and struggling with production previews, but successfully resolved with help from community.
6 replies
Last updated: Jan 30, 2023
R
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):
https://sitename.netlify.app/page-preview/${document._id} ``
https://sitename.netlify.app/product-preview/${document._id} ``
https://sitename.netlify.app/post-preview/${document._id} ``
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
R
Okay, to be more precise, in the migration docs it has this example:
How can I modify this if I just want to query by document._id rather than slug?
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 }, }, })
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
R
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
R
This is all working now. I left out the:
Fully upgraded to v3 now, thanks again for the support!
return prev
Jan 23, 2023, 12:24 PM
R
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.