Skip to content

Commit

Permalink
add react router error handling and redirecting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
goldensunliu committed Jan 31, 2016
1 parent 12042fd commit d6885b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/routes.js
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import { IndexRoute, Route } from 'react-router';
import { IndexRoute, Route, Redirect } from 'react-router';
import fetch from './core/fetch';
import App from './components/App';
import ContentPage from './components/ContentPage';
Expand All @@ -26,6 +26,7 @@ async function getContextComponent(location, callback) {

export default (
<Route>
<Redirect from="/company" to="/about" />
<Route path="/" component={App}>
<IndexRoute getComponent={getContextComponent} />
<Route path="contact" component={ContactPage} />
Expand Down
8 changes: 8 additions & 0 deletions src/server.js
Expand Up @@ -37,6 +37,14 @@ server.use('/api/content', require('./api/content').default);
server.get('*', async (req, res, next) => {
try {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
throw error;
}
if (redirectLocation) {
let redirectPath = `${ redirectLocation.pathname }${ redirectLocation.search }`;
res.redirect(302, redirectPath);
return
}
let statusCode = 200;
const data = { title: '', description: '', css: '', body: '', entry: assets.main.js };
const css = [];
Expand Down

0 comments on commit d6885b9

Please sign in to comment.