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

83 lines
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';
2016-06-12 12:11:05 +09:00
import { IndexRoute, Route, useRouterHistory, browserHistory, createMemoryHistory } from 'react-router';
2016-06-10 19:19:32 +09:00
import { ReactRouterSSR } from 'meteor/reactrouter:react-router-ssr';
2016-06-12 12:11:05 +09:00
import createBrowserHistory from 'history/lib/createBrowserHistory';
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:base-apollo';
import { store } from "./store.js";
Telescope.routes.indexRoute = { name: "posts.list", component: Telescope.components.PostsHome };
2016-06-10 19:19:32 +09:00
Meteor.startup(() => {
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.PostsSingle},
{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},
]);
const ProvidedApp = (props) => (
<ApolloProvider store={store} client={client}>
<Telescope.components.AppContainer {...props} />
</ApolloProvider>
);
2016-06-13 16:02:27 +09:00
const AppRoutes = {
path: '/',
component: ProvidedApp,
indexRoute: Telescope.routes.indexRoute,
childRoutes: Telescope.routes.routes
};
2016-06-14 10:01:44 +09:00
2016-06-12 12:11:05 +09:00
const clientOptions = {
renderHook: ReactDOM.render,
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);
},
// see https://github.com/thereactivestack/meteor-react-router-ssr/blob/9762f12c5d5512c5cfee8663a29428f7e4c141f8/lib/server.jsx#L241-L257
fetchDataHook: (components) => {
console.log('this is where ssr & apollo should interact')
return [new Promise((resolve, reject) => {
resolve();
})];
},
};
ReactRouterSSR.Run(AppRoutes, clientOptions, serverOptions);
// note: we did like this at first
// let history;
// if (Meteor.isClient) {
// history = useNamedRoutes(useRouterHistory(createBrowserHistory))({ routes: AppRoutes });
// }
// if (Meteor.isServer) {
// history = useNamedRoutes(useRouterHistory(createMemoryHistory))({ routes: AppRoutes });
// }
// ReactRouterSSR.Run(AppRoutes, {historyHook: () => history}, {historyHook: () => history});
});