Finding the current Sanity API version for preview setup
vX,
v1, and anything with a date string version. Meaning, all of the date strings are actually using the same version.
I am trying to set up a basic preview using
_const_ client = _await_ getClient({ apiVersion: '2023-01-01' })sanity-plugin-iframe-pane.. I have the following:
`content-preview.js`:
import Iframe from 'sanity-plugin-iframe-pane'
// list of schema types supporting preview
const previewSchemaTypes = ['homePage', 'project', 'post', 'page']
// Default document node:
// Add preview view if part of list
export const defaultDocumentNode = (S, { schemaType }) => {
const frontendUrl = '<http://localhost:3000>'
if (previewSchemaTypes.includes(schemaType)) {
return S.document().views([
S.view.form(),
S.view
.component(Iframe)
.title('Preview')
.options({
url: (doc) => resolveProductionUrl({ doc, context: S.context, frontendUrl }),
defaultSize: 'desktop',
reload: {
button: true
},
attributes: {
allow: 'fullscreen'
}
})
])
}
}
// Resolve production url
export const resolveProductionUrl = async ({ doc, context, frontendUrl }) => {
const { getClient } = context
if (!doc) {
doc = context.document
}
if (previewSchemaTypes.includes(doc._type)) {
const client = await getClient({ apiVersion: '2023-01-01' })
const slug = await client.fetch(`*[_id == $id][0].slug.current`, { id: doc._id })
// Build preview url
const url = new URL(frontendUrl)
// Switch for resolving doc type urls
switch (doc._type) {
case 'homePage':
url.pathname = `/`
break
case 'page':
url.pathname = `/${slug}`
break
case 'project':
url.pathname = `/projects/${slug}`
break
case 'post':
url.pathname = `/posts/${slug}`
break
default:
break
}
// Add preview url params
url.searchParams.set('preview', 'true')
return url.toString()
}
return ''
}sanity.config.jsfile:
// Previews
import { defaultDocumentNode } from './config/content-preview'
import { resolveProductionUrl } from './config/content-preview'
export default defineConfig({
... // client stuff
plugins: [
deskTool({
structure,
defaultDocumentNode
})
],
document: {
productionUrl: async (prev, context) =>
resolveProductionUrl({
context,
frontendUrl: '<http://localhost:3000>'
})
}
})Was this answer helpful?
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.