Skip to content

Commit

Permalink
added example razzle + react router 3 (#539)
Browse files Browse the repository at this point in the history
* added example razzle + react router 3

* Remove yarn.lock

* Fix pkg.json
  • Loading branch information
davidnguyen11 authored and jaredpalmer committed Jun 18, 2018
1 parent db7b28b commit 05d795e
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/with-react-router-3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
logs
*.log
npm-debug.log*
.DS_Store

coverage
node_modules
build
.env.local
.env.development.local
.env.test.local
.env.production.local
19 changes: 19 additions & 0 deletions examples/with-react-router-3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Razzle x React Router 3

## How to use
Download the example [or clone the whole project](https://github.com/jaredpalmer/razzle.git):

```bash
curl https://codeload.github.com/jaredpalmer/razzle/tar.gz/master | tar -xz --strip=2 razzle-master/examples/with-react-router-3
cd with-react-router-3
```

Install it and run:

```bash
yarn install
yarn start
```

## Idea behind the example
This is a basic, bare-bones example of how to use Razzle and React Router 3.
22 changes: 22 additions & 0 deletions examples/with-react-router-3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "razzle-examples-with-react-router-3",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"start": "razzle start",
"build": "razzle build",
"test": "razzle test --env=jsdom",
"prestart:prod": "razzle build",
"start:prod": "NODE_ENV=production node build/server.js"
},
"dependencies": {
"express": "^4.16.2",
"prop-types": "^15.6.1",
"razzle": "^0.8.12",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^3.2.0",
"react-router-redux": "4.0.8",
"react-router-scroll": "0.4.2"
}
}
Binary file added examples/with-react-router-3/public/favicon.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions examples/with-react-router-3/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *

23 changes: 23 additions & 0 deletions examples/with-react-router-3/src/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { Link } from 'react-router';
import App from './App';
import './Home.css';

class About extends React.Component {
render() {
return (
<App>
<div className="Home">
<div className="Home-header">
<h2>About page</h2>
</div>
<p>
<Link to="home">Home</Link>
</p>
</div>
</App>
);
}
}

export default About;
5 changes: 5 additions & 0 deletions examples/with-react-router-3/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
10 changes: 10 additions & 0 deletions examples/with-react-router-3/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import './App.css';

class App extends React.Component {
render() {
return <div>{this.props.children}</div>;
}
}

export default App;
37 changes: 37 additions & 0 deletions examples/with-react-router-3/src/Home.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.Home {
text-align: center;
}

.Home-logo {
animation: Home-logo-spin infinite 20s linear;
height: 80px;
}

.Home-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.Home-intro {
font-size: large;
}

.Home-resources {
list-style: none;
}

.Home-resources > li {
display: inline-block;
padding: 1rem;
}

@keyframes Home-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
40 changes: 40 additions & 0 deletions examples/with-react-router-3/src/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { Link } from 'react-router';
import logo from './react.svg';
import App from './App';
import './Home.css';

class Home extends React.Component {
render() {
return (
<App>
<div className="Home">
<div className="Home-header">
<img src={logo} className="Home-logo" alt="logo" />
<h2>Welcome to Razzle</h2>
</div>
<p className="Home-intro">
To get started, edit <code>src/App.js</code> or{' '}
<code>src/Home.js</code> and save to reload.
</p>
<p>
<Link to="about">About</Link>
</p>
<ul className="Home-resources">
<li>
<a href="https://github.com/jaredpalmer/razzle">Docs</a>
</li>
<li>
<a href="https://github.com/jaredpalmer/razzle/issues">Issues</a>
</li>
<li>
<a href="https://palmer.chat">Community Slack</a>
</li>
</ul>
</div>
</App>
);
}
}

export default Home;
9 changes: 9 additions & 0 deletions examples/with-react-router-3/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import Routes from './routes';
import { hydrate } from 'react-dom';

hydrate(<Routes />, document.getElementById('root'));

if (module.hot) {
module.hot.accept();
}
28 changes: 28 additions & 0 deletions examples/with-react-router-3/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import app from './server';
import http from 'http';

const server = http.createServer(app);

let currentApp = app;

const port = process.env.PORT || 3000;

server.listen(port, error => {
if (error) {
console.log(error);
}

console.log(`🚀 started at: http://localhost:${port}`);
});

if (module.hot) {
console.log('✅ Server-side HMR Enabled!');

module.hot.accept('./server', () => {
console.log('🔁 HMR Reloading `./server`...');
server.removeListener('request', currentApp);
const newApp = require('./server').default;
server.on('request', newApp);
currentApp = newApp;
});
}
6 changes: 6 additions & 0 deletions examples/with-react-router-3/src/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions examples/with-react-router-3/src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { Router, Route, browserHistory } from 'react-router';

import Home from './Home';
import About from './About';

const Routes = props => {
return (
<Router history={browserHistory} {...props}>
<Route path="/" component={Home} />
<Route path="home" component={Home} />
<Route path="about" component={About} />
</Router>
);
};

export default Routes;
67 changes: 67 additions & 0 deletions examples/with-react-router-3/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import express from 'express';
import { match, RouterContext } from 'react-router';
import { renderToString } from 'react-dom/server';

import getRoutes from './routes';

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const server = express();
server
.disable('x-powered-by')
.use(express.static(process.env.RAZZLE_PUBLIC_DIR));

server.use((req, res) => {
match(
{ routes: getRoutes(), location: req.url },
(error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
// You can also check renderProps.components or renderProps.routes for
// your "not found" component or route respectively, and send a 404 as
// below, if you're using a catch-all route.
const context = {};
const markup = renderToString(<RouterContext {...renderProps} />);

if (context.url) {
res.redirect(context.url);
} else {
res.status(200).send(
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8" />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${
assets.client.css
? `<link rel="stylesheet" href="${assets.client.css}">`
: ''
}
${
process.env.NODE_ENV === 'production'
? `<script src="${assets.client.js}" defer></script>`
: `<script src="${
assets.client.js
}" defer crossorigin></script>`
}
</head>
<body>
<div id="root">${markup}</div>
</body>
</html>`,
);
}
} else {
res.status(404).send('Not found');
}
},
);
});

export default server;

0 comments on commit 05d795e

Please sign in to comment.