@nuxtjs/prismic 1.4.2 → 3.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,169 +0,0 @@
1
- import Vue from 'vue'
2
- import Prismic from '@prismicio/client'
3
- import PrismicDOM from 'prismic-dom'
4
-
5
- import linkResolver from '../link-resolver.js'
6
- import htmlSerializer from '../html-serializer.js'
7
-
8
- export default async (context, inject) => {
9
- const { req, route, res, query, redirect, nuxtState, base } = context
10
- let options = {}
11
-
12
- // Pass through server requests, primarily for preview
13
- if (process.server) {
14
- options.req = req
15
- }
16
-
17
- let api = {}
18
- try {
19
- <% if (options.modern) { %>
20
- api = Prismic.client('<%= options.endpoint %>', Object.assign({}, options, <%= JSON.stringify(options.apiOptions) %>))
21
- <% } else { %>
22
- api = await Prismic.api('<%= options.endpoint %>', Object.assign({}, options, <%= JSON.stringify(options.apiOptions) %>))
23
- <% } %>
24
- } catch (error) {
25
- console.error(error)
26
- console.error("Failed to init Prismic API, preventing app fatal error.")
27
- }
28
-
29
- let prismic = new Vue({
30
- computed: {
31
- api() {
32
- return api
33
- },
34
- apiEndpoint() {
35
- return '<%= options.endpoint %>'
36
- },
37
- predicate() {
38
- return Prismic.Predicates
39
- },
40
- predicates() {
41
- return Prismic.Predicates
42
- },
43
- dom() {
44
- return PrismicDOM
45
- }
46
- },
47
- methods: {
48
- asHTML(richText) {
49
- return this.asHtml(richText)
50
- },
51
- asHtml(richText) {
52
- if (richText) {
53
- return PrismicDOM.RichText.asHtml(
54
- richText,
55
- linkResolver,
56
- htmlSerializer
57
- )
58
- }
59
- },
60
- asText(richText) {
61
- if (richText) {
62
- return PrismicDOM.RichText.asText(richText)
63
- }
64
- },
65
- asLink(link) {
66
- if (link) {
67
- return PrismicDOM.Link.url(
68
- link,
69
- linkResolver
70
- )
71
- }
72
- },
73
- asDate(date) {
74
- if (date) {
75
- return PrismicDOM.Date(date)
76
- }
77
- },
78
- <% if (options.preview) { %>
79
- async preview() {
80
- let url = '/'
81
- const { token, documentId } = query
82
-
83
- if (token) {
84
- const previewResolver = await this.api.getPreviewResolver(token, documentId)
85
- const maybeUrl = await previewResolver.resolve(this.linkResolver, '/')
86
- if (maybeUrl) {
87
- url = maybeUrl
88
- }
89
- }
90
- if (process.server) {
91
- redirect(302, url)
92
- } else {
93
- // Add the base path onto the url to preview
94
- url = `${base.replace(/\/$/, '')}${url}`
95
-
96
- window.location.replace(url)
97
- }
98
- },<% } %>
99
- ...(linkResolver && { linkResolver }),
100
- ...(htmlSerializer && { htmlSerializer })
101
- }
102
- })
103
-
104
- inject('prismic', prismic)
105
- context.$prismic = prismic
106
-
107
- <% if (options.preview) { %>
108
- // Load prismic script after Nuxt app is mounted
109
- if (process.client) {
110
- window.<%= globals.readyCallback %> && window.<%= globals.readyCallback %>((app) => {
111
- const script = document.createElement('script')
112
-
113
- script.src = '<%= options.script %>'
114
- document.body.appendChild(script)
115
-
116
- <% if (options.previewReloadType === 'hot') { %>
117
- window.addEventListener('prismicPreviewUpdate', async (event) => {
118
- if (app && 'refresh' in app && typeof app.refresh === 'function') {
119
- event.preventDefault();
120
- if (app.$store && app.$store._actions.nuxtServerInit) {
121
- await app.$store.dispatch('nuxtServerInit', app.$options.context)
122
- }
123
- await app.refresh();
124
- }
125
- });
126
- <% } %>
127
- })
128
- }
129
- // Preview mode
130
- if (process.server && !process.static && route.path === '<%= options.preview %>') {
131
- // Server side
132
- await prismic.preview()
133
- }
134
- if (process.client && process.static && route.path !== '<%= options.preview %>') {
135
- // Client side
136
- const getPreviewCookie = function () {
137
- var value = `; ${document.cookie}`
138
- var parts = value.split(`; ${Prismic.previewCookie}=`)
139
- if (parts.length !== 2) return null
140
- let cookie = parts.pop().split(';').shift()
141
- try {
142
- cookie = JSON.parse(decodeURIComponent(cookie))
143
- } catch (e) {
144
- cookie = null
145
- }
146
- return cookie
147
- }
148
- const repo = '<%= options.repo %>'
149
- const previewCookie = getPreviewCookie()
150
- // Used in prismic_preview middleware
151
- prismic.isPreview = previewCookie && previewCookie[`${repo}.prismic.io`] && previewCookie[`${repo}.prismic.io`].preview
152
-
153
- // Refresh data from Prismic preview
154
- if (prismic.isPreview) {
155
- console.info('[@nuxtjs/prismic] Reload page data for preview')
156
- if (context.enablePreview) {
157
- context.enablePreview()
158
- } else {
159
- // Legacy static preview
160
- window.<%= globals.readyCallback %>(async (app) => {
161
- if (app.$store && app.$store._actions.nuxtServerInit) {
162
- await app.$store.dispatch('nuxtServerInit', app.$options.context)
163
- }
164
- await app.refresh()
165
- })
166
- }
167
- }
168
- }<% } %>
169
- }