保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
import Vue from 'vue'
import { hasFetch, normalizeError, addLifecycleHook, purifyData, createGetCounter } from '../utils'
 
async function serverPrefetch() {
  if (!this._fetchOnServer) {
    return
  }
 
  // Call and await on $fetch
  try {
    await this.$options.fetch.call(this)
  } catch (err) {
    if (process.dev) {
      console.error('Error in fetch():', err)
    }
    this.$fetchState.error = normalizeError(err)
  }
  this.$fetchState.pending = false
 
  // Define an ssrKey for hydration
  this._fetchKey = this._fetchKey || this.$ssrContext.fetchCounters['']++
 
  // Add data-fetch-key on parent element of Component
  const attrs = this.$vnode.data.attrs = this.$vnode.data.attrs || {}
  attrs['data-fetch-key'] = this._fetchKey
 
  // Add to ssrContext for window.__NUXT__.fetch
 
  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)
}
 
export default {
  created() {
    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
    }
 
    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 ? key + ':' + getCounter(key) : String(getCounter(key))
    }
 
    // Added for remove vue undefined warning while ssr
    this.$fetch = () => {} // issue #8043
    Vue.util.defineReactive(this, '$fetchState', {
      pending: true,
      error: null,
      timestamp: Date.now()
    })
 
    addLifecycleHook(this, 'serverPrefetch', serverPrefetch)
  }
}