index.js 9.1 KB

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