Vulcan/packages/nova-base-routes/lib/routes.jsx

101 lines
3.3 KiB
React
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-04-22 10:46:09 +09:00
import React from 'react';
2016-06-19 15:17:44 +09:00
import { Messages } from 'meteor/nova:core';
import { IndexRoute, Route } from 'react-router';
2016-06-10 19:19:32 +09:00
import { ReactRouterSSR } from 'meteor/reactrouter:react-router-ssr';
2016-06-23 15:16:32 +09:00
import Events from "meteor/nova:events";
2016-06-29 09:15:52 +02:00
import Helmet from 'react-helmet';
import Cookie from 'react-cookie';
import ReactDOM from 'react-dom';
2016-06-10 19:19:32 +09:00
import { ApolloProvider } from 'react-apollo';
import { client } from 'meteor/nova:apollo';
import { configureStore } from "./store.js";
2016-06-10 19:19:32 +09:00
Meteor.startup(() => {
/*
Routes definition
*/
Telescope.routes.indexRoute = { name: "posts.list", component: Telescope.components.PostsList };
2016-06-11 16:36:18 +09:00
Telescope.routes.add([
2016-07-27 13:06:57 +09:00
{name:"posts.daily", path:"daily", component:Telescope.components.PostsDaily},
{name:"posts.single", path:"posts/:_id(/:slug)", component:Telescope.components.PostsPage},
{name:"users.single", path:"users/:slug", component:Telescope.components.UsersSingle},
{name:"users.account", path:"account", component:Telescope.components.UsersAccount},
2016-09-16 16:14:23 +02:00
{name:"users.edit", path:"users/:slug/edit", component:Telescope.components.UsersAccount},
{name:"app.notfound", path:"*", component:Telescope.components.Error404},
]);
2016-06-13 16:02:27 +09:00
const AppRoutes = {
path: '/',
component: Telescope.components.AppContainer,
indexRoute: Telescope.routes.indexRoute,
childRoutes: Telescope.routes.routes
};
2016-06-14 10:01:44 +09:00
/*
Hooks client side and server side definition
*/
let history;
let initialState;
let store;
// Use history hook to get a reference to the history object
const historyHook = newHistory => history = newHistory;
// Pass the state of the store as the object to be dehydrated server side
const dehydrateHook = () => {
2016-11-07 11:46:58 +09:00
// console.log('store get state', store.getState());
return store.getState();
}
// Take the rehydrated state and use it as the initial state client side
const rehydrateHook = state => {
2016-11-07 11:46:58 +09:00
// console.log('rehydrated state', state);
initialState = state
};
2016-06-12 12:11:05 +09:00
const clientOptions = {
historyHook,
rehydrateHook,
wrapperHook(app) {
store = configureStore(initialState, history);
return <ApolloProvider store={store} client={client}>{app}</ApolloProvider>
},
props: {
onUpdate: () => {
Events.analyticsRequest();
// clear all previous messages
store.dispatch(Telescope.actions.messages.clearSeen());
},
},
};
2016-06-12 12:11:05 +09:00
const serverOptions = {
htmlHook: (html) => {
const head = Helmet.rewind();
return html.replace('<head>', '<head>'+ head.title + head.meta + head.link);
},
preRender: (req, res) => {
Cookie.plugToRequest(req, res);
},
historyHook,
dehydrateHook,
// see https://github.com/thereactivestack/meteor-react-router-ssr/blob/9762f12c5d5512c5cfee8663a29428f7e4c141f8/lib/server.jsx#L241-L257
// note: can't get it working well
// fetchDataHook: (components) => {
// console.log(components[0]); // = Apollo(AppContainer)
// // console.log('this is where ssr & apollo should interact -> fetch data')
// return [components[0].fetchData({} /* should be props .. how to get them?*/, {client})];
// },
fetchDataHook: (components) => components,
};
ReactRouterSSR.Run(AppRoutes, clientOptions, serverOptions);
});