Skip to content

Instantly share code, notes, and snippets.

@mornir
Last active July 6, 2020 13:10
Show Gist options
  • Save mornir/ec2b631ffdb13205111a5244c3d3d881 to your computer and use it in GitHub Desktop.
Save mornir/ec2b631ffdb13205111a5244c3d3d881 to your computer and use it in GitHub Desktop.
Preview content from Sanity, to be placed at the root of the pages directory
<template>
<component :is="currentComponent"
v-if="draft"
:person="draft"
:page="draft" />
<p v-else>{{ msg }}</p>
</template>
<script>
import PageDetails from '@/components/PageDetails'
import PersonDetails from '@/components/PersonDetails'
import preview from '@/sanity/sanityPreview'
import { previews } from '@/sanity/queries'
export default {
name: 'Preview',
head() {
return {
meta: [{ hid: 'robots', name: 'robots', content: 'noindex, nofollow' }],
}
},
components: {
PageDetails,
PersonDetails,
},
data() {
return {
draft: {},
currentComponent: '',
query: '',
msg: 'Preview is loading...',
}
},
async mounted() {
if (this.$route.query.preview_id && this.$route.query.component) {
this.currentComponent = this.$route.query.component + 'Details'
this.query = previews['preview' + this.$route.query.component]
const params = {
id: this.$route.query.preview_id,
}
this.draft = await preview.fetch(this.query, params).catch(e => {
console.error(e)
this.msg =
'An error occured. No preview possible. Please try again to open the preview from the CMS.'
})
} else {
console.info('"preview_id" and/or "component" parameters are missing')
this.msg = 'There's no preview for this page.'
}
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment