index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import Vue from 'vue'
  2. import Meta from 'vue-meta'
  3. import ClientOnly from 'vue-client-only'
  4. import NoSsr from 'vue-no-ssr'
  5. import { createRouter } from './router.js'
  6. import NuxtChild from './components/nuxt-child.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_plugin_00dbd983 from 'nuxt_plugin_plugin_00dbd983' // Source: .\\components\\plugin.js (mode: 'all')
  13. import nuxt_plugin_elementui_d905880e from 'nuxt_plugin_elementui_d905880e' // Source: ..\\plugins\\element-ui (mode: 'all')
  14. // Component: <ClientOnly>
  15. Vue.component(ClientOnly.name, ClientOnly)
  16. // TODO: Remove in Nuxt 3: <NoSsr>
  17. Vue.component(NoSsr.name, {
  18. ...NoSsr,
  19. render (h, ctx) {
  20. if (process.client && !NoSsr._warned) {
  21. NoSsr._warned = true
  22. console.warn('<no-ssr> has been deprecated and will be removed in Nuxt 3, please use <client-only> instead')
  23. }
  24. return NoSsr.render(h, ctx)
  25. }
  26. })
  27. // Component: <NuxtChild>
  28. Vue.component(NuxtChild.name, NuxtChild)
  29. Vue.component('NChild', NuxtChild)
  30. // Component NuxtLink is imported in server.js or client.js
  31. // Component: <Nuxt>
  32. Vue.component(Nuxt.name, Nuxt)
  33. Object.defineProperty(Vue.prototype, '$nuxt', {
  34. get() {
  35. const globalNuxt = this.$root.$options.$nuxt
  36. if (process.client && !globalNuxt && typeof window !== 'undefined') {
  37. return window.$nuxt
  38. }
  39. return globalNuxt
  40. },
  41. configurable: true
  42. })
  43. Vue.use(Meta, {"keyName":"head","attribute":"data-n-head","ssrAttribute":"data-n-head-ssr","tagIDKeyName":"hid"})
  44. const defaultTransition = {"name":"page","mode":"out-in","appear":false,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  45. async function createApp(ssrContext, config = {}) {
  46. const router = await createRouter(ssrContext, config)
  47. // Create Root instance
  48. // here we inject the router and store to all child components,
  49. // making them available everywhere as `this.$router` and `this.$store`.
  50. const app = {
  51. head: {"title":"gs_nuxt","htmlAttrs":{"lang":"en"},"meta":[{"charset":"utf-8"},{"name":"viewport","content":"width=device-width, initial-scale=1"},{"hid":"description","name":"description","content":""},{"name":"format-detection","content":"telephone=no"}],"link":[{"rel":"icon","type":"image\u002Fx-icon","href":"\u002Ffavicon.ico"}],"style":[],"script":[]},
  52. router,
  53. nuxt: {
  54. defaultTransition,
  55. transitions: [defaultTransition],
  56. setTransitions (transitions) {
  57. if (!Array.isArray(transitions)) {
  58. transitions = [transitions]
  59. }
  60. transitions = transitions.map((transition) => {
  61. if (!transition) {
  62. transition = defaultTransition
  63. } else if (typeof transition === 'string') {
  64. transition = Object.assign({}, defaultTransition, { name: transition })
  65. } else {
  66. transition = Object.assign({}, defaultTransition, transition)
  67. }
  68. return transition
  69. })
  70. this.$options.nuxt.transitions = transitions
  71. return transitions
  72. },
  73. err: null,
  74. dateErr: null,
  75. error (err) {
  76. err = err || null
  77. app.context._errored = Boolean(err)
  78. err = err ? normalizeError(err) : null
  79. let nuxt = app.nuxt // to work with @vue/composition-api, see https://github.com/nuxt/nuxt.js/issues/6517#issuecomment-573280207
  80. if (this) {
  81. nuxt = this.nuxt || this.$options.nuxt
  82. }
  83. nuxt.dateErr = Date.now()
  84. nuxt.err = err
  85. // Used in src/server.js
  86. if (ssrContext) {
  87. ssrContext.nuxt.error = err
  88. }
  89. return err
  90. }
  91. },
  92. ...App
  93. }
  94. const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  95. // Resolve route
  96. let route
  97. if (ssrContext) {
  98. route = router.resolve(ssrContext.url).route
  99. } else {
  100. const path = getLocation(router.options.base, router.options.mode)
  101. route = router.resolve(path).route
  102. }
  103. // Set context to app.context
  104. await setContext(app, {
  105. route,
  106. next,
  107. error: app.nuxt.error.bind(app),
  108. payload: ssrContext ? ssrContext.payload : undefined,
  109. req: ssrContext ? ssrContext.req : undefined,
  110. res: ssrContext ? ssrContext.res : undefined,
  111. beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined,
  112. ssrContext
  113. })
  114. function inject(key, value) {
  115. if (!key) {
  116. throw new Error('inject(key, value) has no key provided')
  117. }
  118. if (value === undefined) {
  119. throw new Error(`inject('${key}', value) has no value provided`)
  120. }
  121. key = '$' + key
  122. // Add into app
  123. app[key] = value
  124. // Add into context
  125. if (!app.context[key]) {
  126. app.context[key] = value
  127. }
  128. // Check if plugin not already installed
  129. const installKey = '__nuxt_' + key + '_installed__'
  130. if (Vue[installKey]) {
  131. return
  132. }
  133. Vue[installKey] = true
  134. // Call Vue.use() to install the plugin into vm
  135. Vue.use(() => {
  136. if (!Object.prototype.hasOwnProperty.call(Vue.prototype, key)) {
  137. Object.defineProperty(Vue.prototype, key, {
  138. get () {
  139. return this.$root.$options[key]
  140. }
  141. })
  142. }
  143. })
  144. }
  145. // Inject runtime config as $config
  146. inject('config', config)
  147. // Add enablePreview(previewData = {}) in context for plugins
  148. if (process.static && process.client) {
  149. app.context.enablePreview = function (previewData = {}) {
  150. app.previewData = Object.assign({}, previewData)
  151. inject('preview', previewData)
  152. }
  153. }
  154. // Plugin execution
  155. if (typeof nuxt_plugin_plugin_00dbd983 === 'function') {
  156. await nuxt_plugin_plugin_00dbd983(app.context, inject)
  157. }
  158. if (typeof nuxt_plugin_elementui_d905880e === 'function') {
  159. await nuxt_plugin_elementui_d905880e(app.context, inject)
  160. }
  161. // Lock enablePreview in context
  162. if (process.static && process.client) {
  163. app.context.enablePreview = function () {
  164. console.warn('You cannot call enablePreview() outside a plugin.')
  165. }
  166. }
  167. // Wait for async component to be resolved first
  168. await new Promise((resolve, reject) => {
  169. // Ignore 404s rather than blindly replacing URL in browser
  170. if (process.client) {
  171. const { route } = router.resolve(app.context.route.fullPath)
  172. if (!route.matched.length) {
  173. return resolve()
  174. }
  175. }
  176. router.replace(app.context.route.fullPath, resolve, (err) => {
  177. // https://github.com/vuejs/vue-router/blob/v3.4.3/src/util/errors.js
  178. if (!err._isRouter) return reject(err)
  179. if (err.type !== 2 /* NavigationFailureType.redirected */) return resolve()
  180. // navigated to a different route in router guard
  181. const unregister = router.afterEach(async (to, from) => {
  182. if (process.server && ssrContext && ssrContext.url) {
  183. ssrContext.url = to.fullPath
  184. }
  185. app.context.route = await getRouteData(to)
  186. app.context.params = to.params || {}
  187. app.context.query = to.query || {}
  188. unregister()
  189. resolve()
  190. })
  191. })
  192. })
  193. return {
  194. app,
  195. router
  196. }
  197. }
  198. export { createApp, NuxtError }