2020-02-04 18:36:22 +00:00
|
|
|
import Vue from 'vue'
|
2020-12-17 11:49:59 +00:00
|
|
|
import { hasFetch, normalizeError, addLifecycleHook, purifyData, createGetCounter } from '../utils'
|
2020-02-04 18:36:22 +00:00
|
|
|
|
|
|
|
async function serverPrefetch() {
|
|
|
|
if (!this._fetchOnServer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call and await on $fetch
|
|
|
|
try {
|
|
|
|
await this.$options.fetch.call(this)
|
|
|
|
} catch (err) {
|
2020-05-07 19:08:01 +00:00
|
|
|
if (process.dev) {
|
|
|
|
console.error('Error in fetch():', err)
|
|
|
|
}
|
2020-02-04 18:36:22 +00:00
|
|
|
this.$fetchState.error = normalizeError(err)
|
|
|
|
}
|
|
|
|
this.$fetchState.pending = false
|
|
|
|
|
|
|
|
|
|
|
|
// Define an ssrKey for hydration
|
2020-12-17 11:49:59 +00:00
|
|
|
this._fetchKey = this._fetchKey || this.$ssrContext.fetchCounters['']++
|
2020-02-04 18:36:22 +00:00
|
|
|
|
2020-02-12 15:41:23 +00:00
|
|
|
// Add data-fetch-key on parent element of Component
|
2020-02-04 18:36:22 +00:00
|
|
|
const attrs = this.$vnode.data.attrs = this.$vnode.data.attrs || {}
|
2020-02-12 15:41:23 +00:00
|
|
|
attrs['data-fetch-key'] = this._fetchKey
|
2020-02-04 18:36:22 +00:00
|
|
|
|
2020-02-12 15:41:23 +00:00
|
|
|
// Add to ssrContext for window.__NUXT__.fetch
|
2020-12-17 11:49:59 +00:00
|
|
|
<% if (debug) { %>
|
|
|
|
if (this.$ssrContext.nuxt.fetch[this._fetchKey] !== undefined) {
|
|
|
|
console.warn(`Duplicate fetch key detected (${this._fetchKey}). This may lead to unexpected results.`)
|
|
|
|
}
|
|
|
|
<% } %>
|
|
|
|
this.$ssrContext.nuxt.fetch[this._fetchKey] =
|
|
|
|
this.$fetchState.error ? { _error: this.$fetchState.error } : purifyData(this._data)
|
2020-02-04 18:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2020-05-07 19:08:01 +00:00
|
|
|
created() {
|
2020-02-04 18:36:22 +00:00
|
|
|
if (!hasFetch(this)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.$options.fetchOnServer === 'function') {
|
|
|
|
this._fetchOnServer = this.$options.fetchOnServer.call(this) !== false
|
|
|
|
} else {
|
|
|
|
this._fetchOnServer = this.$options.fetchOnServer !== false
|
|
|
|
}
|
|
|
|
|
2020-12-17 11:49:59 +00:00
|
|
|
const defaultKey = this.$options._scopeId || this.$options.name || ''
|
|
|
|
const getCounter = createGetCounter(this.$ssrContext.fetchCounters, defaultKey)
|
|
|
|
|
|
|
|
if (typeof this.$options.fetchKey === 'function') {
|
|
|
|
this._fetchKey = this.$options.fetchKey.call(this, getCounter)
|
|
|
|
} else {
|
|
|
|
const key = 'string' === typeof this.$options.fetchKey ? this.$options.fetchKey : defaultKey
|
|
|
|
this._fetchKey = key + getCounter(key)
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:16:00 +00:00
|
|
|
// Added for remove vue undefined warning while ssr
|
|
|
|
this.$fetch = () => {} // issue #8043
|
2020-02-04 18:36:22 +00:00
|
|
|
Vue.util.defineReactive(this, '$fetchState', {
|
|
|
|
pending: true,
|
|
|
|
error: null,
|
|
|
|
timestamp: Date.now()
|
|
|
|
})
|
|
|
|
|
|
|
|
addLifecycleHook(this, 'serverPrefetch', serverPrefetch)
|
|
|
|
}
|
|
|
|
}
|