entry-server.js 975 Bytes
import {createApp} from './app';
import {
    SET_ENV
} 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 && url !== route.redirectedFrom) {
            reject({url: route.fullPath});
        }
        store.commit(SET_ENV, context.env);
        router.push(url);
        router.onReady(() => {
            const matched = router.getMatchedComponents();

            if (!matched.length) {
                reject({code: 404});
            }
            Promise.all(matched.map(({asyncData}) =>
                asyncData && asyncData({store, router: router.currentRoute})))
                .then(() => {
                    context.state = store.state;
                    resolve(app);
                }).catch(reject);
        }, reject);
    });
};