entry-server.js
2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {createApp} from './app';
import _ from 'lodash/core';
import {
SET_ENV,
INIT_ROUTE_CHANGE
} from 'store/yoho/types';
export default context => {
return new Promise((resolve, reject) => {
const {app, router, store} = createApp(context);
const {url} = context;
const route = router.resolve(url).route;
// if (url !== route.fullPath) {
// return reject({code: 500, message: 'url not matched', url: route.fullPath});
// }
store.commit(SET_ENV, context.env);
router.push(url);
router.onReady(() => {
try {
const matched = router.getMatchedComponents();
if (!matched.length) {
reject({code: 404});
}
const routes = [];
const rootRoute = _.find(router.options.routes, r => r.meta && r.meta.root);
if (rootRoute) {
routes.push({
name: rootRoute.name,
fullPath: rootRoute.path
});
}
if (route.name !== 'channel.home') {
routes.push({
name: route.name,
fullPath: route.fullPath
});
}
store.commit(INIT_ROUTE_CHANGE, {routes});
Promise.all(matched.map(({asyncData}) =>
asyncData && asyncData({store, router: router.currentRoute})))
.then(() => {
context.state = store.state;
resolve(app);
}).catch((e) => {
reject({
code: 500,
message: e.stack || e.toString()
});
});
} catch (e) {
reject({
code: 500,
message: e.stack || e.toString()
});
}
}, reject);
});
};