2018-10-03 00:07:01 +03:00
|
|
|
import _get from 'lodash/get'
|
2018-09-02 23:26:15 +03:00
|
|
|
|
|
|
|
import { createApp } from './app'
|
|
|
|
|
|
|
|
const { app, router, store } = createApp()
|
|
|
|
|
|
|
|
const STATE_KEY = '__INITIAL_STATE__'
|
|
|
|
|
|
|
|
if (window[STATE_KEY]) {
|
|
|
|
store.replaceState(window[STATE_KEY])
|
|
|
|
}
|
|
|
|
|
|
|
|
router.onReady(() => {
|
2019-01-04 21:34:16 +03:00
|
|
|
registerBeforeResolve()
|
2018-09-02 23:26:15 +03:00
|
|
|
app.$mount('#app')
|
|
|
|
})
|
|
|
|
|
2019-02-04 10:57:28 +03:00
|
|
|
function registerBeforeResolve () {
|
2019-02-10 15:00:50 +03:00
|
|
|
router.beforeEach(async (to, from, next) => {
|
2019-02-15 14:52:31 +03:00
|
|
|
// This case handles navigation to anchors on same page
|
|
|
|
if (to.path === from.path) {
|
|
|
|
next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-10 15:00:50 +03:00
|
|
|
store.commit('tooglePageLoading')
|
|
|
|
if (!to.matched.length) {
|
|
|
|
store.commit('tooglePageLoading')
|
|
|
|
next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const propsOption = to.matched[0].props.default
|
|
|
|
const props = propsOption
|
|
|
|
? typeof propsOption === 'function'
|
|
|
|
? propsOption(to)
|
|
|
|
: typeof propsOption === 'object'
|
|
|
|
? propsOption
|
|
|
|
: to.params
|
|
|
|
: {}
|
|
|
|
const routeComponent = to.matched[0].components.default
|
|
|
|
const matchedRootComponent = routeComponent.cid // Check if component already imported
|
|
|
|
? routeComponent
|
|
|
|
: (await routeComponent()).default
|
|
|
|
const matchedComponentsAndChildren = getComponentAndItsChildren(matchedRootComponent)
|
|
|
|
await Promise.all(matchedComponentsAndChildren.map(component => {
|
2019-02-19 23:24:07 +03:00
|
|
|
const serverPrefetch = component.options.serverPrefetch && component.options.serverPrefetch[0]
|
|
|
|
if (typeof serverPrefetch === 'function') {
|
|
|
|
return serverPrefetch.call({
|
2019-02-10 15:00:50 +03:00
|
|
|
$store: store,
|
|
|
|
$router: router,
|
|
|
|
...props
|
2018-10-18 11:56:56 +03:00
|
|
|
})
|
|
|
|
}
|
2019-02-10 15:00:50 +03:00
|
|
|
}))
|
|
|
|
store.commit('tooglePageLoading')
|
|
|
|
next()
|
2018-09-02 23:26:15 +03:00
|
|
|
})
|
|
|
|
}
|
2018-10-03 00:07:01 +03:00
|
|
|
|
2019-02-04 10:57:28 +03:00
|
|
|
function getComponentAndItsChildren (component, result?) {
|
2018-10-03 00:07:01 +03:00
|
|
|
if (!result) {
|
|
|
|
result = []
|
|
|
|
}
|
|
|
|
if (!result.includes(component)) {
|
|
|
|
result.push(component)
|
|
|
|
}
|
2019-02-10 15:00:50 +03:00
|
|
|
const children = Object.values(component.options.components)
|
2018-10-03 00:07:01 +03:00
|
|
|
// Parent component is also presents in components object
|
|
|
|
.filter(x => x !== component)
|
|
|
|
children.forEach(x => getComponentAndItsChildren(x, result))
|
|
|
|
|
|
|
|
return result
|
2019-02-04 10:57:28 +03:00
|
|
|
}
|