1 | import Vue from 'vue'
|
2 | <% utilsImports = [
|
3 | ...(features.asyncData || features.fetch) ? [
|
4 | 'getMatchedComponentsInstances',
|
5 | 'getChildrenComponentInstancesUsingFetch',
|
6 | 'promisify',
|
7 | 'globalHandleError',
|
8 | 'urlJoin'
|
9 | ] : [],
|
10 | ...features.layouts ? [
|
11 | 'sanitizeComponent'
|
12 | ]: []
|
13 | ] %>
|
14 | <% if (utilsImports.length) { %>import { <%= utilsImports.join(', ') %> } from './utils'<% } %>
|
15 | import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>'
|
16 | <% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./components/nuxt-loading.vue") %>'<% } %>
|
17 | <% if (buildIndicator) { %>import NuxtBuildIndicator from './components/nuxt-build-indicator'<% } %>
|
18 | <% css.forEach((c) => { %>
|
19 | import '<%= relativeToBuild(resolvePath(c.src || c, { isStyle: true })) %>'
|
20 | <% }) %>
|
21 |
|
22 | <% if (features.layouts) { %>
|
23 | <%= Object.keys(layouts).map((key) => {
|
24 | if (splitChunks.layouts) {
|
25 | return `const _${hash(key)} = () => import('${layouts[key]}' /* webpackChunkName: "${wChunk('layouts/' + key)}" */).then(m => sanitizeComponent(m.default || m))`
|
26 | } else {
|
27 | return `import _${hash(key)} from '${layouts[key]}'`
|
28 | }
|
29 | }).join('\n') %>
|
30 |
|
31 | <% if (splitChunks.layouts) { %>
|
32 | let resolvedLayouts = {}
|
33 | const layouts = { <%= Object.keys(layouts).map(key => `"_${key}": _${hash(key)}`).join(',') %> }<%= isTest ? '// eslint-disable-line' : '' %>
|
34 | <% } else { %>
|
35 | const layouts = { <%= Object.keys(layouts).map(key => `"_${key}": sanitizeComponent(_${hash(key)})`).join(',') %> }<%= isTest ? '// eslint-disable-line' : '' %>
|
36 | <% } %>
|
37 |
|
38 | <% } %>
|
39 |
|
40 | export default {
|
41 | render (h, props) {
|
42 | <% if (loading) { %>const loadingEl = h('NuxtLoading', { ref: 'loading' })<% } %>
|
43 | <% if (features.layouts) { %>
|
44 | const layoutEl = h(this.layout || 'nuxt')
|
45 | const templateEl = h('div', {
|
46 | domProps: {
|
47 | id: '__layout'
|
48 | },
|
49 | key: this.layoutName
|
50 | }, [layoutEl])
|
51 | <% } else { %>
|
52 | const templateEl = h('nuxt')
|
53 | <% } %>
|
54 |
|
55 | <% if (features.transitions) { %>
|
56 | const transitionEl = h('transition', {
|
57 | props: {
|
58 | name: '<%= layoutTransition.name %>',
|
59 | mode: '<%= layoutTransition.mode %>'
|
60 | },
|
61 | on: {
|
62 | beforeEnter (el) {
|
63 |
|
64 | window.<%= globals.nuxt %>.$nextTick(() => {
|
65 | window.<%= globals.nuxt %>.$emit('triggerScroll')
|
66 | })
|
67 | }
|
68 | }
|
69 | }, [templateEl])
|
70 | <% } %>
|
71 |
|
72 | return h('div', {
|
73 | domProps: {
|
74 | id: '<%= globals.id %>'
|
75 | }
|
76 | }, [
|
77 | <% if (loading) { %>loadingEl, <% } %>
|
78 | <% if (buildIndicator) { %>h(NuxtBuildIndicator), <% } %>
|
79 | <% if (features.transitions) { %>transitionEl<% } else { %>templateEl<% } %>
|
80 | ])
|
81 | },
|
82 | <% if (features.clientOnline || features.layouts) { %>
|
83 | data: () => ({
|
84 | <% if (features.clientOnline) { %>
|
85 | isOnline: true,
|
86 | <% } %>
|
87 | <% if (features.layouts) { %>
|
88 | layout: null,
|
89 | layoutName: '',
|
90 | <% } %>
|
91 | <% if (features.fetch) { %>
|
92 | nbFetching: 0
|
93 | <% } %>
|
94 | }),
|
95 | <% } %>
|
96 | beforeCreate () {
|
97 | Vue.util.defineReactive(this, 'nuxt', this.$options.nuxt)
|
98 | },
|
99 | created () {
|
100 |
|
101 | Vue.prototype.<%= globals.nuxt %> = this
|
102 | if (process.client) {
|
103 |
|
104 | window.<%= globals.nuxt %> = <%= (globals.nuxt !== '$nuxt' ? 'window.$nuxt = ' : '') %>this
|
105 | <% if (features.clientOnline) { %>
|
106 | this.refreshOnlineStatus()
|
107 | // Setup the listeners
|
108 | window.addEventListener('online', this.refreshOnlineStatus)
|
109 | window.addEventListener('offline', this.refreshOnlineStatus)
|
110 | <% } %>
|
111 | }
|
112 | // Add $nuxt.error()
|
113 | this.error = this.nuxt.error
|
114 | // Add $nuxt.context
|
115 | this.context = this.$options.context
|
116 | },
|
117 | <% if (loading || isFullStatic) { %>
|
118 | async mounted () {
|
119 | <% if (loading) { %>this.$loading = this.$refs.loading<% } %>
|
120 | <% if (isFullStatic) {%>
|
121 | if (this.isPreview) {
|
122 | if (this.$store && this.$store._actions.nuxtServerInit) {
|
123 | <% if (loading) { %>this.$loading.start()<% } %>
|
124 | await this.$store.dispatch('nuxtServerInit', this.context)
|
125 | }
|
126 | await this.refresh()
|
127 | <% if (loading) { %>this.$loading.finish()<% } %>
|
128 | }
|
129 | <% } %>
|
130 | },
|
131 | <% } %>
|
132 | watch: {
|
133 | 'nuxt.err': 'errorChanged'
|
134 | },
|
135 | <% if (features.clientOnline) { %>
|
136 | computed: {
|
137 | isOffline () {
|
138 | return !this.isOnline
|
139 | },
|
140 | <% if (features.fetch) { %>
|
141 | isFetching () {
|
142 | return this.nbFetching > 0
|
143 | },<% } %>
|
144 | <% if (nuxtOptions.target === 'static') { %>
|
145 | isPreview () {
|
146 | return Boolean(this.$options.previewData)
|
147 | },<% } %>
|
148 | },
|
149 | <% } %>
|
150 | methods: {
|
151 | <%= isTest ? '/* eslint-disable comma-dangle */' : '' %>
|
152 | <% if (features.clientOnline) { %>
|
153 | refreshOnlineStatus () {
|
154 | if (process.client) {
|
155 | if (typeof window.navigator.onLine === 'undefined') {
|
156 | // If the browser doesn't support connection status reports
|
157 | // assume that we are online because most apps' only react
|
158 | // when they now that the connection has been interrupted
|
159 | this.isOnline = true
|
160 | } else {
|
161 | this.isOnline = window.navigator.onLine
|
162 | }
|
163 | }
|
164 | },
|
165 | <% } %>
|
166 | async refresh () {
|
167 | <% if (features.asyncData || features.fetch) { %>
|
168 | const pages = getMatchedComponentsInstances(this.$route)
|
169 |
|
170 | if (!pages.length) {
|
171 | return
|
172 | }
|
173 | <% if (loading) { %>this.$loading.start()<% } %>
|
174 |
|
175 | const promises = pages.map((page) => {
|
176 | const p = []
|
177 |
|
178 | <% if (features.fetch) { %>
|
179 | // Old fetch
|
180 | if (page.$options.fetch && page.$options.fetch.length) {
|
181 | p.push(promisify(page.$options.fetch, this.context))
|
182 | }
|
183 | if (page.$fetch) {
|
184 | p.push(page.$fetch())
|
185 | } else {
|
186 | // Get all component instance to call $fetch
|
187 | for (const component of getChildrenComponentInstancesUsingFetch(page.$vnode.componentInstance)) {
|
188 | p.push(component.$fetch())
|
189 | }
|
190 | }
|
191 | <% } %>
|
192 | <% if (features.asyncData) { %>
|
193 | if (page.$options.asyncData) {
|
194 | p.push(
|
195 | promisify(page.$options.asyncData, this.context)
|
196 | .then((newData) => {
|
197 | for (const key in newData) {
|
198 | Vue.set(page.$data, key, newData[key])
|
199 | }
|
200 | })
|
201 | )
|
202 | }
|
203 | <% } %>
|
204 | return Promise.all(p)
|
205 | })
|
206 | try {
|
207 | await Promise.all(promises)
|
208 | } catch (error) {
|
209 | <% if (loading) { %>this.$loading.fail(error)<% } %>
|
210 | globalHandleError(error)
|
211 | this.error(error)
|
212 | }
|
213 | <% if (loading) { %>this.$loading.finish()<% } %>
|
214 | <% } %>
|
215 | },
|
216 | <% if (splitChunks.layouts) { %>async <% } %>errorChanged () {
|
217 | if (this.nuxt.err) {
|
218 | <% if (loading) { %>
|
219 | if (this.$loading) {
|
220 | if (this.$loading.fail) {
|
221 | this.$loading.fail(this.nuxt.err)
|
222 | }
|
223 | if (this.$loading.finish) {
|
224 | this.$loading.finish()
|
225 | }
|
226 | }
|
227 | <% } %>
|
228 | let errorLayout = (NuxtError.options || NuxtError).layout;
|
229 |
|
230 | if (typeof errorLayout === 'function') {
|
231 | errorLayout = errorLayout(this.context)
|
232 | }
|
233 | <% if (splitChunks.layouts) { %>
|
234 | await this.loadLayout(errorLayout)
|
235 | <% } %>
|
236 | this.setLayout(errorLayout)
|
237 | }
|
238 | },
|
239 | <% if (features.layouts) { %>
|
240 | <% if (splitChunks.layouts) { %>
|
241 | setLayout (layout) {
|
242 | <% if (debug) { %>
|
243 | if(layout && typeof layout !== 'string') {
|
244 | throw new Error('[nuxt] Avoid using non-string value as layout property.')
|
245 | }
|
246 | <% } %>
|
247 | if (!layout || !resolvedLayouts['_' + layout]) {
|
248 | layout = 'default'
|
249 | }
|
250 | this.layoutName = layout
|
251 | let _layout = '_' + layout
|
252 | this.layout = resolvedLayouts[_layout]
|
253 | return this.layout
|
254 | },
|
255 | loadLayout (layout) {
|
256 | const undef = !layout
|
257 | const nonexistent = !(layouts['_' + layout] || resolvedLayouts['_' + layout])
|
258 | let _layout = '_' + ((undef || nonexistent) ? 'default' : layout)
|
259 | if (resolvedLayouts[_layout]) {
|
260 | return Promise.resolve(resolvedLayouts[_layout])
|
261 | }
|
262 | return layouts[_layout]()
|
263 | .then((Component) => {
|
264 | resolvedLayouts[_layout] = Component
|
265 | delete layouts[_layout]
|
266 | return resolvedLayouts[_layout]
|
267 | })
|
268 | .catch((e) => {
|
269 | if (this.<%= globals.nuxt %>) {
|
270 | return this.<%= globals.nuxt %>.error({ statusCode: 500, message: e.message })
|
271 | }
|
272 | })
|
273 | },
|
274 | <% } else { %>
|
275 | setLayout (layout) {
|
276 | <% if (debug) { %>
|
277 | if(layout && typeof layout !== 'string') {
|
278 | throw new Error('[nuxt] Avoid using non-string value as layout property.')
|
279 | }
|
280 | <% } %>
|
281 | if (!layout || !layouts['_' + layout]) {
|
282 | layout = 'default'
|
283 | }
|
284 | this.layoutName = layout
|
285 | this.layout = layouts['_' + layout]
|
286 | return this.layout
|
287 | },
|
288 | loadLayout (layout) {
|
289 | if (!layout || !layouts['_' + layout]) {
|
290 | layout = 'default'
|
291 | }
|
292 | return Promise.resolve(layouts['_' + layout])
|
293 | },
|
294 | <% } /* splitChunks.layouts */ %>
|
295 | <% } /* features.layouts */ %>
|
296 | <% if (isFullStatic) { %>
|
297 | setPagePayload(payload) {
|
298 | this._pagePayload = payload
|
299 | this._payloadFetchIndex = 0
|
300 | },
|
301 | async fetchPayload(route) {
|
302 | const { staticAssetsBase } = window.<%= globals.context %>
|
303 | const base = (this.$router.options.base || '').replace(/\/+$/, '')
|
304 | if (base && route.startsWith(base)) {
|
305 | route = route.substr(base.length)
|
306 | }
|
307 | route = (route.replace(/\/+$/, '') || '/').split('?')[0].split('#')[0]
|
308 | const src = urlJoin(base, staticAssetsBase, route, 'payload.js')
|
309 | try {
|
310 | const payload = await window.__NUXT_IMPORT__(decodeURI(route), encodeURI(src))
|
311 | this.setPagePayload(payload)
|
312 | return payload
|
313 | } catch (err) {
|
314 | this.setPagePayload(false)
|
315 | throw err
|
316 | }
|
317 | }
|
318 | <% } %>
|
319 | },
|
320 | <% if (loading) { %>
|
321 | components: {
|
322 | NuxtLoading
|
323 | }
|
324 | <% } %>
|
325 | <%= isTest ? '/* eslint-enable comma-dangle */' : '' %>
|
326 | }
|
327 |
|
\ | No newline at end of file |