entry-server.js 1.02 KB
import {createApp} from './app';
import createReport from 'report-error';

import {
  SET_ENV,
  ROUTE_CHANGE
} from 'store/yoho/types';


export default context => {
  const reportError = createReport(context);

  return new Promise((resolve, reject) => {
    const {app, router, store} = createApp(context);
    const {url} = context;

    store.commit(SET_ENV, {context});
    router.push(url);
    router.onReady(() => {
      const matched = router.getMatchedComponents();


      store.commit(ROUTE_CHANGE, {to: router.currentRoute});

      if (matched.some(m => !m)) {
        reportError(new Error('导航组件为空'));
        router.push({name: 'error.500'});
        return resolve(app);
      }
      if (!matched.length) {
        return reject({code: 404, message: ''});
      }
      context.rendered = () => {
        context.state = store.state;
      };
      return resolve(app);
    });

    router.onError(e => {
      reportError(e);
      router.push({name: 'error.500'});
      return resolve(app);
    });
  });
};