Skip to content

Instantly share code, notes, and snippets.

@stursby
Last active July 10, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stursby/efb36fd1ea7f38c8ad2b8329c32d9e17 to your computer and use it in GitHub Desktop.
Save stursby/efb36fd1ea7f38c8ad2b8329c32d9e17 to your computer and use it in GitHub Desktop.
Craft 3.2 Headless Preview
  1. This assumes you’ve got Craft 3.2 running and the CraftQL plugin installed 🙌🏻

  2. Create a Section named Home and then attach a field named Copy to your field layout.

  3. Add a Preview Taget (in the Home section settings) with the label Local pointing to the URL http://localhost:3000

image

  1. Go into the Home entry and click Preview button, then switch to Local target.

craft32-nuxt

<template>
<div>
<h1>Craft 3.2 Client</h1>
<h3>Hello, from Nuxt.</h3>
<p>{{ entry.copy }}</p>
</div>
</template>
<script>
import axios from 'axios'
const token = 'YOUR_CRAFTQL_TOKEN'
const headers = {
Authorization: `Bearer ${token}`
}
const query = `
{
entry(section:[home]) {
...on Home {
copy
}
}
}
`
const api = axios.create({
baseURL: 'http://craft-32.test/api',
headers
})
export default {
async asyncData({ route }) {
const token = route.query.token || ''
const { data } = await api.post(`/?token=${token}`, { query })
return {
entry: data.data.entry
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment