2016-12-12 11:00:25 +00:00
|
|
|
import { Components, Actions } from 'meteor/nova:lib';
|
2016-04-22 10:46:09 +09:00
|
|
|
import React from 'react';
|
2016-06-10 19:19:32 +09:00
|
|
|
import { ReactRouterSSR } from 'meteor/reactrouter:react-router-ssr';
|
2016-06-29 09:15:52 +02:00
|
|
|
import Helmet from 'react-helmet';
|
2016-08-17 18:13:19 +02:00
|
|
|
import Cookie from 'react-cookie';
|
2016-08-25 09:54:24 +09:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-11-16 16:17:12 +01:00
|
|
|
import ApolloClient from 'apollo-client';
|
2016-12-12 09:55:24 +09:00
|
|
|
import { getDataFromTree, ApolloProvider } from 'react-apollo';
|
2016-11-16 16:17:12 +01:00
|
|
|
import { meteorClientConfig } from 'meteor/nova:apollo';
|
2017-01-04 14:31:38 +01:00
|
|
|
import { runCallbacks, addRoute, Routes, configureStore, addReducer, addMiddleware } from 'meteor/nova:core';
|
2016-10-19 10:32:24 +02:00
|
|
|
|
2016-11-21 16:18:08 +09:00
|
|
|
Meteor.startup(function initNovaRoutesAndApollo() {
|
2016-06-11 16:36:18 +09:00
|
|
|
|
2017-01-03 16:42:53 +08:00
|
|
|
addRoute({name:"app.notfound", path:"*", component:Components.Error404});
|
2016-12-12 11:12:41 +09:00
|
|
|
|
|
|
|
const indexRoute = _.filter(Routes, route => route.path === '/')[0];
|
|
|
|
const childRoutes = _.reject(Routes, route => route.path === '/');
|
|
|
|
|
|
|
|
delete indexRoute.path; // delete the '/' path to avoid warning
|
2016-06-13 16:02:27 +09:00
|
|
|
|
2016-06-15 11:07:30 +09:00
|
|
|
const AppRoutes = {
|
|
|
|
path: '/',
|
2016-12-06 18:06:29 +01:00
|
|
|
component: Components.App,
|
2016-12-12 11:12:41 +09:00
|
|
|
indexRoute,
|
|
|
|
childRoutes,
|
2016-10-19 10:32:24 +02:00
|
|
|
};
|
2016-06-14 10:01:44 +09:00
|
|
|
|
2016-11-01 11:45:44 +01:00
|
|
|
/*
|
|
|
|
Hooks client side and server side definition
|
|
|
|
*/
|
|
|
|
|
2017-01-03 16:42:53 +08:00
|
|
|
|
2016-11-01 11:45:44 +01:00
|
|
|
let history;
|
|
|
|
let initialState;
|
|
|
|
let store;
|
2016-11-16 16:17:12 +01:00
|
|
|
let client;
|
2017-01-03 16:42:53 +08:00
|
|
|
|
2016-11-01 11:45:44 +01:00
|
|
|
// Use history hook to get a reference to the history object
|
|
|
|
const historyHook = newHistory => history = newHistory;
|
|
|
|
|
2016-08-17 18:13:19 +02:00
|
|
|
const clientOptions = {
|
2016-11-01 11:45:44 +01:00
|
|
|
historyHook,
|
2016-11-16 16:17:12 +01:00
|
|
|
rehydrateHook: state => {
|
2016-11-16 16:26:53 +01:00
|
|
|
// console.log('rehydrated state', state);
|
2016-11-16 16:17:12 +01:00
|
|
|
initialState = state
|
|
|
|
},
|
|
|
|
wrapperHook(app, loginToken) {
|
2016-11-16 16:26:53 +01:00
|
|
|
// console.log('wrapper hook initial state', initialState);
|
2017-01-04 14:31:38 +01:00
|
|
|
// configure apollo
|
2016-11-16 16:17:12 +01:00
|
|
|
client = new ApolloClient(meteorClientConfig({cookieLoginToken: loginToken}));
|
2017-01-04 14:31:38 +01:00
|
|
|
const reducers = addReducer({apollo: client.reducer()});
|
|
|
|
const middleware = addMiddleware(client.middleware());
|
|
|
|
|
|
|
|
// configure the redux store
|
|
|
|
store = configureStore(reducers, initialState, middleware);
|
2016-11-16 16:34:37 +01:00
|
|
|
|
2016-11-01 11:45:44 +01:00
|
|
|
return <ApolloProvider store={store} client={client}>{app}</ApolloProvider>
|
|
|
|
},
|
2016-08-17 18:13:19 +02:00
|
|
|
props: {
|
|
|
|
onUpdate: () => {
|
2016-12-13 11:40:24 +09:00
|
|
|
runCallbacks('router.onUpdate');
|
2016-10-31 17:13:14 +01:00
|
|
|
// clear all previous messages
|
2016-12-12 16:43:23 +09:00
|
|
|
store.dispatch(Actions.messages.clearSeen());
|
2016-10-31 17:13:14 +01:00
|
|
|
},
|
|
|
|
},
|
2016-08-17 18:13:19 +02:00
|
|
|
};
|
2016-06-12 12:11:05 +09:00
|
|
|
|
2016-08-17 18:13:19 +02:00
|
|
|
const serverOptions = {
|
2016-11-16 16:17:12 +01:00
|
|
|
historyHook,
|
2016-08-17 18:13:19 +02:00
|
|
|
htmlHook: (html) => {
|
|
|
|
const head = Helmet.rewind();
|
2017-01-03 16:42:53 +08:00
|
|
|
return html.replace('<head>', '<head>'+ head.title + head.meta + head.link);
|
2016-08-17 18:13:19 +02:00
|
|
|
},
|
2016-11-16 16:17:12 +01:00
|
|
|
preRender: (req, res, app) => {
|
2016-12-09 13:26:46 +01:00
|
|
|
Cookie.plugToRequest(req, res);
|
2016-11-16 16:17:12 +01:00
|
|
|
//console.log('preRender hook', app);
|
|
|
|
// console.log(req.cookies);
|
|
|
|
return Promise.await(getDataFromTree(app));
|
2016-08-17 18:13:19 +02:00
|
|
|
},
|
2016-11-16 16:17:12 +01:00
|
|
|
dehydrateHook: () => {
|
2016-11-16 16:26:53 +01:00
|
|
|
// console.log(client.store.getState());
|
2016-11-16 16:17:12 +01:00
|
|
|
return client.store.getState();
|
|
|
|
},
|
|
|
|
// fetchDataHook: (components) => components,
|
2016-08-17 18:13:19 +02:00
|
|
|
};
|
2017-01-03 16:42:53 +08:00
|
|
|
|
2016-08-17 18:13:19 +02:00
|
|
|
ReactRouterSSR.Run(AppRoutes, clientOptions, serverOptions);
|
2016-12-12 11:00:25 +00:00
|
|
|
});
|