Skip to content

Instantly share code, notes, and snippets.

@jonykrause
Created May 4, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonykrause/a267165df4dd7689575511b6e65215fd to your computer and use it in GitHub Desktop.
Save jonykrause/a267165df4dd7689575511b6e65215fd to your computer and use it in GitHub Desktop.
universal react-router
// SHARED
const routes = (
<Route path="/base" component={Layout}>
<IndexRoute component={A} />
<Route path="/base/foo" component={B} />
<Route path="*" component={C} />
</Route>
);
// SERVER
// render.js
const memoryHistory = createMemoryHistory(req.url);
const history = syncHistoryWithStore(memoryHistory, store);
match({ history, routes, location: req.originalUrl }, (err, redirectLocation, renderProps) => {
res.send(renderPage(content, store));
});
//server.js
express().use("/base", render.js)
// CLIENT
const history = syncHistoryWithStore(browserHistory, store);
render(
<Provider store={store}>
<Router history={history} routes={routes} />
</Provider>,
document.getElementById("app")
);
@jonykrause
Copy link
Author

jonykrause commented May 4, 2016

// react-router 2.3.0
Server side rendering works with this setup although its kinda lame to repeat "/base" over and over again. When enabling JS client side the following error is produced as I’m redirected to "/"
bildschirmfoto 2016-05-04 um 16 16 37

@jonykrause
Copy link
Author

Besides that (Basename is something I could not get to work by trying to enable it server side with const memoryHistory = useRouterHistory(createMemoryHistory)({ entries: ["/"], basename: "/base" }); leads to <a href="/base/base">in html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment