2017-02-06 14:33:34 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Helmet from 'react-helmet';
|
|
|
|
import { getDataFromTree, ApolloProvider } from 'react-apollo';
|
|
|
|
// import styleSheet from 'styled-components/lib/models/StyleSheet';
|
|
|
|
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Components,
|
|
|
|
addRoute,
|
|
|
|
Routes, populateComponentsApp, populateRoutesApp,
|
2017-02-08 04:36:57 +08:00
|
|
|
getRenderContext,
|
2017-06-14 09:53:42 +09:00
|
|
|
dynamicLoader,
|
2017-03-29 15:48:44 +09:00
|
|
|
} from 'meteor/vulcan:lib';
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
import { RouterServer } from './router.jsx';
|
|
|
|
|
|
|
|
Meteor.startup(() => {
|
|
|
|
// note: route defined here because it "shouldn't be removable"
|
|
|
|
addRoute({name:"app.notfound", path:"*", componentName: 'Error404'});
|
|
|
|
|
|
|
|
// init the application components and routes, including components & routes from 3rd-party packages
|
|
|
|
populateComponentsApp();
|
|
|
|
populateRoutesApp();
|
|
|
|
|
|
|
|
const indexRoute = _.filter(Routes, route => route.path === '/')[0];
|
|
|
|
const childRoutes = _.reject(Routes, route => route.path === '/');
|
2017-03-11 11:43:28 +08:00
|
|
|
|
2017-02-17 20:45:11 +09:00
|
|
|
if (indexRoute) {
|
|
|
|
delete indexRoute.path; // delete the '/' path to avoid warning
|
|
|
|
}
|
2017-02-06 14:33:34 +08:00
|
|
|
|
2017-06-14 09:53:42 +09:00
|
|
|
// go through each route and if it's a promise, wrap it with dynamicLoader
|
|
|
|
_.forEach(childRoutes, (route, routeName) => {
|
|
|
|
if (route.component && typeof route.component.then === 'function') {
|
|
|
|
route.component = dynamicLoader(route.component);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-06 14:33:34 +08:00
|
|
|
const AppRoutes = {
|
|
|
|
path: '/',
|
|
|
|
component: Components.App,
|
|
|
|
indexRoute,
|
|
|
|
childRoutes,
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
historyHook(req, res, newHistory) {
|
2017-02-08 09:15:09 +08:00
|
|
|
const { history } = getRenderContext();
|
|
|
|
return history;
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
wrapperHook(req, res, appGenerator) {
|
2017-02-12 22:30:52 +08:00
|
|
|
const { apolloClient, store } = getRenderContext();
|
2017-03-11 11:43:28 +08:00
|
|
|
store.reload();
|
|
|
|
store.dispatch({ type: '@@nova/INIT' }) // the first dispatch will generate a newDispatch function from middleware
|
2017-02-06 14:33:34 +08:00
|
|
|
const app = appGenerator();
|
2017-02-12 22:30:52 +08:00
|
|
|
return <ApolloProvider store={store} client={apolloClient}>{app}</ApolloProvider>;
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
preRender(req, res, app) {
|
|
|
|
return Promise.await(getDataFromTree(app));
|
|
|
|
},
|
|
|
|
dehydrateHook(req, res) {
|
2017-02-08 04:36:57 +08:00
|
|
|
const context = getRenderContext();
|
2017-02-07 16:10:55 +01:00
|
|
|
return context.apolloClient.store.getState();
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
postRender(req, res) {
|
|
|
|
// req.css = styleSheet.sheet ? styleSheet.rules().map(rule => rule.cssText).join('\n') : '';
|
|
|
|
// const context = renderContext.get();
|
|
|
|
// context.css = req.css;
|
|
|
|
},
|
|
|
|
htmlHook(req, res, dynamicHead, dynamicBody) {
|
|
|
|
const head = Helmet.rewind();
|
|
|
|
return {
|
2017-02-15 21:06:04 +08:00
|
|
|
dynamicHead: `${head.title}${head.meta}${head.link}${head.script}${dynamicHead}`,
|
2017-02-06 14:33:34 +08:00
|
|
|
dynamicBody,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
RouterServer.run(AppRoutes, options);
|
|
|
|
});
|