UNPKG

2.25 kBJavaScriptView Raw
1import Vue from 'vue'
2import { createApp } from './app.js'
3import { callAsyncRecursive } from './ssr/callAsyncRecursive.js'
4import { registerStoreModules } from './ssr/registerStoreModules.js'
5import root from 'src/app.vue'
6import Progress from 'src/components/progress.vue'
7
8// Register global progress bar
9const progress = Vue.prototype.$progress = new Vue(Progress).$mount()
10document.body.appendChild(progress.$el)
11
12// Create main Vue instance
13const { app, router, store } = createApp()
14
15// prime the store with server-initialized state.
16// the state is determined during SSR and inlined in the page markup.
17if (window.__INITIAL_STATE__) {
18 store.replaceState(window.__INITIAL_STATE__)
19}
20
21// set access & refresh token of client-side api, delete after against possible exploitation
22if (store.state.$access_token) {
23 app.$cubic.setAccessToken(store.state.$access_token)
24 delete store.state.$access_token
25}
26if (store.state.$refresh_token) {
27 app.$cubic.setRefreshToken(store.state.$refresh_token)
28 delete store.state.$refresh_token
29}
30
31// Wait until router has resolved possible async hooks
32router.onReady(() => {
33 const routerView = router.getMatchedComponents()
34
35 // Register dynamic store components on direct page load. We'll need to
36 // pass a true value for the third param of the register function here to
37 // check if we already got our state server-sided.
38 registerStoreModules(root, store)
39 routerView.map(component => registerStoreModules(component, store, true))
40
41 // Add router hook for handling asyncData.
42 // Doing it after initial route is resolved so that we don't double-fetch
43 // the data that we already have. Using router.beforeResolve() so that all
44 // async components are resolved.
45 router.beforeResolve(async (to, from, next) => {
46 const matched = router.getMatchedComponents(to)
47
48 // Register dyanmic store modules on route change (not direct load!)
49 registerStoreModules(root, store)
50 matched.map(component => registerStoreModules(component, store, true))
51
52 // Call asyncData
53 let progressStarted
54 await Promise.all(matched.map(c => callAsyncRecursive(c, store, router, to, progress, progressStarted)))
55
56 // End loading bar
57 progress.finish()
58 next()
59 })
60 app.$mount('#app')
61})