index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import Vue from 'vue'
  2. import Meta from 'vue-meta'
  3. import { createRouter } from './router.js'
  4. import NoSSR from './components/no-ssr.js'
  5. import NuxtChild from './components/nuxt-child.js'
  6. import NuxtLink from './components/nuxt-link.js'
  7. import NuxtError from './components/nuxt-error.vue'
  8. import Nuxt from './components/nuxt.js'
  9. import App from './App.js'
  10. import { setContext, getLocation, getRouteData, normalizeError } from './utils'
  11. /* Plugins */
  12. import nuxt_plugin_axios_0c5c16d5 from 'nuxt_plugin_axios_0c5c16d5' // Source: ./axios.js
  13. import nuxt_plugin_elementui_d905880e from 'nuxt_plugin_elementui_d905880e' // Source: ../plugins/element-ui
  14. import nuxt_plugin_http_926ab708 from 'nuxt_plugin_http_926ab708' // Source: ../plugins/http
  15. import nuxt_plugin_quill_23090991 from 'nuxt_plugin_quill_23090991' // Source: ../plugins/quill
  16. // Component: <no-ssr>
  17. Vue.component(NoSSR.name, NoSSR)
  18. // Component: <nuxt-child>
  19. Vue.component(NuxtChild.name, NuxtChild)
  20. // Component: <nuxt-link>
  21. Vue.component(NuxtLink.name, NuxtLink)
  22. // Component: <nuxt>`
  23. Vue.component(Nuxt.name, Nuxt)
  24. // vue-meta configuration
  25. Vue.use(Meta, {
  26. keyName: 'head', // the component option name that vue-meta looks for meta info on.
  27. attribute: 'data-n-head', // the attribute name vue-meta adds to the tags it observes
  28. ssrAttribute: 'data-n-head-ssr', // the attribute name that lets vue-meta know that meta info has already been server-rendered
  29. tagIDKeyName: 'hid' // the property name that vue-meta uses to determine whether to overwrite or append a tag
  30. })
  31. const defaultTransition = {"name":"page","mode":"out-in","appear":true,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  32. async function createApp(ssrContext) {
  33. const router = await createRouter(ssrContext)
  34. // Create Root instance
  35. // here we inject the router and store to all child components,
  36. // making them available everywhere as `this.$router` and `this.$store`.
  37. const app = {
  38. router,
  39. nuxt: {
  40. defaultTransition,
  41. transitions: [ defaultTransition ],
  42. setTransitions(transitions) {
  43. if (!Array.isArray(transitions)) {
  44. transitions = [ transitions ]
  45. }
  46. transitions = transitions.map((transition) => {
  47. if (!transition) {
  48. transition = defaultTransition
  49. } else if (typeof transition === 'string') {
  50. transition = Object.assign({}, defaultTransition, { name: transition })
  51. } else {
  52. transition = Object.assign({}, defaultTransition, transition)
  53. }
  54. return transition
  55. })
  56. this.$options.nuxt.transitions = transitions
  57. return transitions
  58. },
  59. err: null,
  60. dateErr: null,
  61. error(err) {
  62. err = err || null
  63. app.context._errored = !!err
  64. err = err ? normalizeError(err) : null
  65. const nuxt = this.nuxt || this.$options.nuxt
  66. nuxt.dateErr = Date.now()
  67. nuxt.err = err
  68. // Used in src/server.js
  69. if (ssrContext) ssrContext.nuxt.error = err
  70. return err
  71. }
  72. },
  73. ...App
  74. }
  75. const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  76. // Resolve route
  77. let route
  78. if (ssrContext) {
  79. route = router.resolve(ssrContext.url).route
  80. } else {
  81. const path = getLocation(router.options.base)
  82. route = router.resolve(path).route
  83. }
  84. // Set context to app.context
  85. await setContext(app, {
  86. route,
  87. next,
  88. error: app.nuxt.error.bind(app),
  89. payload: ssrContext ? ssrContext.payload : undefined,
  90. req: ssrContext ? ssrContext.req : undefined,
  91. res: ssrContext ? ssrContext.res : undefined,
  92. beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined
  93. })
  94. const inject = function (key, value) {
  95. if (!key) throw new Error('inject(key, value) has no key provided')
  96. if (typeof value === 'undefined') throw new Error('inject(key, value) has no value provided')
  97. key = '$' + key
  98. // Add into app
  99. app[key] = value
  100. // Check if plugin not already installed
  101. const installKey = '__nuxt_' + key + '_installed__'
  102. if (Vue[installKey]) return
  103. Vue[installKey] = true
  104. // Call Vue.use() to install the plugin into vm
  105. Vue.use(() => {
  106. if (!Vue.prototype.hasOwnProperty(key)) {
  107. Object.defineProperty(Vue.prototype, key, {
  108. get() {
  109. return this.$root.$options[key]
  110. }
  111. })
  112. }
  113. })
  114. }
  115. // Plugin execution
  116. if (typeof nuxt_plugin_axios_0c5c16d5 === 'function') await nuxt_plugin_axios_0c5c16d5(app.context, inject)
  117. if (typeof nuxt_plugin_elementui_d905880e === 'function') await nuxt_plugin_elementui_d905880e(app.context, inject)
  118. if (typeof nuxt_plugin_http_926ab708 === 'function') await nuxt_plugin_http_926ab708(app.context, inject)
  119. if (typeof nuxt_plugin_quill_23090991 === 'function') await nuxt_plugin_quill_23090991(app.context, inject)
  120. // If server-side, wait for async component to be resolved first
  121. if (process.server && ssrContext && ssrContext.url) {
  122. await new Promise((resolve, reject) => {
  123. router.push(ssrContext.url, resolve, () => {
  124. // navigated to a different route in router guard
  125. const unregister = router.afterEach(async (to, from, next) => {
  126. ssrContext.url = to.fullPath
  127. app.context.route = await getRouteData(to)
  128. app.context.params = to.params || {}
  129. app.context.query = to.query || {}
  130. unregister()
  131. resolve()
  132. })
  133. })
  134. })
  135. }
  136. return {
  137. app,
  138. router
  139. }
  140. }
  141. export { createApp, NuxtError }