-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
preview-plugin.ts
72 lines (62 loc) · 2.09 KB
/
preview-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { createApp } from 'vue'
import { contentStorage } from '../composables/client-db'
import { defineNuxtPlugin, useRoute, useCookie, refreshNuxtData, useRuntimeConfig } from '#imports'
import { ContentPreviewMode } from '#components'
export default defineNuxtPlugin((nuxt) => {
const { previewAPI } = useRuntimeConfig().public.content
async function fetchData (token: string) {
// Fetch preview data from station
const data = await $fetch('api/projects/preview', {
baseURL: previewAPI,
params: {
token
}
})
// Remove previous preview data
const keys = await contentStorage.getKeys(`${token}:`)
keys.forEach(key => contentStorage.removeItem(key))
// Fill store with preview content
const items = [
...(data.files || []),
...data.additions,
...data.deletions.map(d => ({ ...d, parsed: { _id: d.path.replace(/\//g, ':'), __deleted: true } }))
]
// Set preview meta
await contentStorage.setItem(
`${token}$`,
JSON.stringify({
ignoreBuiltContents: (data.files || []).length !== 0
})
)
for (const item of items) {
await contentStorage.setItem(`${token}:${item.parsed._id}`, JSON.stringify(item.parsed))
}
}
async function initializePreview () {
const query = useRoute().query || {}
const previewToken = useCookie('previewToken', { sameSite: 'none', secure: true })
if (!query.preview && !previewToken.value) {
return
}
if (query.preview && previewToken.value !== query.preview) {
previewToken.value = String(query.preview)
}
// Show loading
const el = document.createElement('div')
el.id = 'nuxt-content-preview'
document.body.appendChild(el)
createApp(ContentPreviewMode, {
previewToken,
apiURL: previewAPI,
onRefresh: () => fetchData(previewToken.value).then(() => refreshNuxtData())
}).mount(el)
await fetchData(previewToken.value)
}
nuxt.hook('app:mounted', async () => {
await initializePreview()
})
nuxt.hook('page:finish', () => {
// Refresh nuxt data
refreshNuxtData()
})
})