2017-02-06 14:33:34 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Helmet from 'react-helmet';
|
|
|
|
import { getDataFromTree, ApolloProvider } from 'react-apollo';
|
|
|
|
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Components,
|
|
|
|
addRoute,
|
2017-06-17 15:24:53 +09:00
|
|
|
Routes, populateComponentsApp, populateRoutesApp, initializeFragments,
|
2017-02-08 04:36:57 +08:00
|
|
|
getRenderContext,
|
2017-10-13 14:20:20 +01:00
|
|
|
runCallbacks,
|
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
|
2017-06-18 10:14:34 +09:00
|
|
|
initializeFragments();
|
2017-02-06 14:33:34 +08:00
|
|
|
populateComponentsApp();
|
|
|
|
populateRoutesApp();
|
2017-06-17 15:24:53 +09:00
|
|
|
|
2017-02-06 14:33:34 +08:00
|
|
|
const indexRoute = _.filter(Routes, route => route.path === '/')[0];
|
|
|
|
const childRoutes = _.reject(Routes, route => route.path === '/');
|
2017-03-11 11:43:28 +08:00
|
|
|
|
2017-09-15 19:13:39 +02:00
|
|
|
const indexRouteWithoutPath = _.clone(indexRoute);
|
2017-09-29 07:40:59 +09:00
|
|
|
|
|
|
|
if (indexRouteWithoutPath) {
|
|
|
|
delete indexRouteWithoutPath.path; // delete path to avoid warning
|
|
|
|
}
|
2017-02-06 14:33:34 +08:00
|
|
|
|
|
|
|
const AppRoutes = {
|
|
|
|
path: '/',
|
|
|
|
component: Components.App,
|
2017-09-15 19:13:39 +02:00
|
|
|
indexRoute: indexRouteWithoutPath,
|
2017-02-06 14:33:34 +08:00
|
|
|
childRoutes,
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
historyHook(req, res, newHistory) {
|
2017-10-13 14:20:20 +01:00
|
|
|
let { history } = getRenderContext();
|
|
|
|
history = runCallbacks('router.server.history', history, { req, res, newHistory });
|
2017-02-08 09:15:09 +08:00
|
|
|
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-10-13 14:20:20 +01:00
|
|
|
const app = runCallbacks('router.server.wrapper', appGenerator(), { req, res, store, apolloClient });
|
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) {
|
2017-10-13 14:20:20 +01:00
|
|
|
runCallbacks('router.server.preRender', { req, res, app });
|
2017-02-06 14:33:34 +08:00
|
|
|
return Promise.await(getDataFromTree(app));
|
|
|
|
},
|
|
|
|
dehydrateHook(req, res) {
|
2017-10-13 14:20:20 +01:00
|
|
|
const context = runCallbacks('router.server.dehydrate', getRenderContext(), { req, res });
|
2017-02-07 16:10:55 +01:00
|
|
|
return context.apolloClient.store.getState();
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
postRender(req, res) {
|
2017-10-13 14:20:20 +01:00
|
|
|
runCallbacks('router.server.postRender', { req, res });
|
2017-02-06 14:33:34 +08:00
|
|
|
},
|
|
|
|
htmlHook(req, res, dynamicHead, dynamicBody) {
|
2017-10-13 14:20:20 +01:00
|
|
|
const head = runCallbacks('router.server.html', Helmet.rewind(), { req, res, dynamicHead, dynamicBody });
|
2017-02-06 14:33:34 +08:00
|
|
|
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);
|
|
|
|
});
|