Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

POC react router 4 #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"homepage": "https://github.com/nytm/wf-kyt-starter-universal#readme",
"kyt": {
"version": "^0.2.0"
"version": "^0.3.0"
},
"dependencies": {
"compression": "^1.6.2",
Expand All @@ -25,6 +25,6 @@
"react": "^15.3.0",
"react-addons-test-utils": "^15.3.0",
"react-dom": "^15.3.0",
"react-router": "^2.6.1"
"react-router": "^4.0.0-alpha.5"
}
}
9 changes: 5 additions & 4 deletions src/client/Root.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

import React from 'react';
import Router from 'react-router/lib/Router';
import browserHistory from 'react-router/lib/browserHistory';
import routes from '../routes';
import { BrowserRouter } from 'react-router';
import App from '../components/App';

// We need a Root component for React Hot Loading.
function Root() {
return (
<Router history={browserHistory} routes={routes} />
<BrowserRouter>
<App />
</BrowserRouter>
);
}

Expand Down
17 changes: 9 additions & 8 deletions src/components/App/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

import React, { PropTypes } from 'react';
import Link from 'react-router/lib/Link';
import React from 'react';
import { Link, Match, Miss } from 'react-router';
import NoMatch from '../NoMatch';
import styles from './styles.scss';
import routes from './routes';

function App({ children }) {
function App() {
return (
<div>
<i className={styles.logo} />
Expand All @@ -16,14 +18,13 @@ function App({ children }) {
</li>
</ul>
<div className={styles.content}>
{children}
{routes.map((route, i) => (
<Match key={i} {...route} />
))}
<Miss component={NoMatch} />
</div>
</div>
);
}

App.propTypes = {
children: PropTypes.node,
};

export default App;
14 changes: 14 additions & 0 deletions src/components/App/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import Home from '../Home';
import Tools from '../Tools';

const routes = [{
exactly: true,
pattern: '/',
component: Home,
}, {
pattern: '/tools',
component: Tools,
}];

export default routes;
10 changes: 10 additions & 0 deletions src/components/NoMatch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import React from 'react';

function NoMatch() {
return (
<h3>No match for <code>{location.pathname}</code></h3>
);
}

export default NotMatch;
40 changes: 0 additions & 40 deletions src/routes/index.js

This file was deleted.

47 changes: 26 additions & 21 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import compression from 'compression';
import path from 'path';
import React from 'react';
import { renderToString } from 'react-dom/server';
import RouterContext from 'react-router/lib/RouterContext';
import createMemoryHistory from 'react-router/lib/createMemoryHistory';
import match from 'react-router/lib/match';
import { ServerRouter, createServerRenderContext } from 'react-router';
import App from '../components/App';
import template from './template';
import routes from '../routes';

const clientAssets = require(KYT.ASSETS_MANIFEST); // eslint-disable-line import/no-dynamic-require
const app = express();
Expand All @@ -24,25 +22,32 @@ app.use(express.static(path.join(process.cwd(), KYT.PUBLIC_DIR)));

// Setup server side routing.
app.get('*', (request, response) => {
const history = createMemoryHistory(request.originalUrl);

match({ routes, history }, (error, redirectLocation, renderProps) => {
if (error) {
response.status(500).send(error.message);
} else if (redirectLocation) {
response.redirect(302, `${redirectLocation.pathname}${redirectLocation.search}`);
} else if (renderProps) {
// When a React Router route is matched then we render
// the components and assets into the template.
response.status(200).send(template({
root: renderToString(<RouterContext {...renderProps} />),
jsBundle: clientAssets.main.js,
cssBundle: clientAssets.main.css,
}));
const context = createServerRenderContext();
let markup = renderToString(
<ServerRouter location={request.url} context={context}>
<App />
</ServerRouter>
);
const result = context.getResult();
if (result.redirect) {
response.redirect(302, `${result.redirect.pathname}${result.redirect.search}`);
} else {
if (result.missed) {
response.status(404);
markup = renderToString(
<ServerRouter location={request.url} context={context}>
<App />
</ServerRouter>
);
} else {
response.status(404).send('Not found');
response.status(200);
}
});
response.send(template({
root: markup,
jsBundle: clientAssets.main.js,
cssBundle: clientAssets.main.css,
}));
}
});

app.listen(parseInt(KYT.SERVER_PORT, 10));