Skip to content

Commit 1873d0f

Browse files
committed
Root other than "/" supported ("sampleapp" is set as an example). "--inline" removed to disable automatic refresh. "webpack-dev-server" host set to "0.0.0.0".
1 parent 718ffec commit 1873d0f

File tree

11 files changed

+45
-17
lines changed

11 files changed

+45
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
React Router Tutorial
22
=====================
33

4+
This forked repository contains changes in [Lesson 13 - Server Rendering](/lessons/13-server-rendering/). That code corresponds actually to the result of [Lesson 12 - Navigating](/lessons/12-navigating/), which can be considered the end of the tutorial if you exclude the Server Rendering part.
5+
6+
The changes enable the deployment in a different root other than "/". That would be useful if you are e.g. composing multiple servers and you want them to have different roots.
7+
8+
--
9+
410
Quick lessons for getting up-to-speed with React Router.
511

612
See [Lesson 1 - Setting Up](/lessons/01-setting-up/) to get started.

lessons/13-server-rendering/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Server Rendering
22

3+
This forked repository contains changes that enable the deployment in a different root other than "/". That would be useful if you are e.g. composing multiple servers and you want them to have different roots. "/sampleapp" is the root used here as an example.
4+
5+
Remember that the code here is actually the one corresponding to the result of [Lesson 12 - Navigating](/lessons/12-navigating/).
6+
7+
--
8+
39
Alright, first things first. Server rendering, at its core is a simple
410
concept in React.
511

lessons/13-server-rendering/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import About from './modules/About'
66
import Repos from './modules/Repos'
77
import Repo from './modules/Repo'
88
import Home from './modules/Home'
9+
import { addRoot } from './modules/Root'
910

1011
render((
1112
<Router history={browserHistory}>
12-
<Route path="/" component={App}>
13+
<Route path={addRoot("/")} component={App}>
1314
<IndexRoute component={Home}/>
14-
<Route path="/repos" component={Repos}>
15-
<Route path="/repos/:userName/:repoName" component={Repo}/>
15+
<Route path={addRoot("/repos")} component={Repos}>
16+
<Route path={addRoot("/repos/:userName/:repoName")} component={Repo}/>
1617
</Route>
17-
<Route path="/about" component={About}/>
18+
<Route path={addRoot("/about")} component={About}/>
1819
</Route>
1920
</Router>
2021
), document.getElementById('app'))

lessons/13-server-rendering/modules/App.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React from 'react'
22
import NavLink from './NavLink'
3+
import { addRoot } from './Root'
34

45
export default React.createClass({
56
render() {
67
return (
78
<div>
89
<h1>React Router Tutorial</h1>
910
<ul role="nav">
10-
<li><NavLink to="/" onlyActiveOnIndex>Home</NavLink></li>
11-
<li><NavLink to="/about">About</NavLink></li>
12-
<li><NavLink to="/repos">Repos</NavLink></li>
11+
<li><NavLink to={addRoot("/")} onlyActiveOnIndex>Home</NavLink></li>
12+
<li><NavLink to={addRoot("/about")}>About</NavLink></li>
13+
<li><NavLink to={addRoot("/repos")}>Repos</NavLink></li>
1314
</ul>
1415
{this.props.children}
1516
</div>

lessons/13-server-rendering/modules/Repos.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import NavLink from './NavLink'
3+
import { addRoot } from './Root'
34

45
export default React.createClass({
56
contextTypes: {
@@ -10,7 +11,7 @@ export default React.createClass({
1011
event.preventDefault()
1112
const userName = event.target.elements[0].value
1213
const repo = event.target.elements[1].value
13-
const path = `/repos/${userName}/${repo}`
14+
const path = addRoot(`/repos/${userName}/${repo}`)
1415
this.context.router.push(path)
1516
},
1617

@@ -19,8 +20,8 @@ export default React.createClass({
1920
<div>
2021
<h2>Repos</h2>
2122
<ul>
22-
<li><NavLink to="/repos/reactjs/react-router">React Router</NavLink></li>
23-
<li><NavLink to="/repos/facebook/react">React</NavLink></li>
23+
<li><NavLink to={addRoot("/repos/reactjs/react-router")}>React Router</NavLink></li>
24+
<li><NavLink to={addRoot("/repos/facebook/react")}>React</NavLink></li>
2425
<li>
2526
<form onSubmit={this.handleSubmit}>
2627
<input type="text" placeholder="userName"/> / {' '}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Do not end with '/'
2+
var root = '/sampleapp'
3+
4+
export function getRoot() {
5+
return root
6+
}
7+
export function addRoot(path) {
8+
return root + path
9+
}

lessons/13-server-rendering/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
8-
"start:dev": "webpack-dev-server --inline --content-base public/ --history-api-fallback",
8+
"start:dev": "webpack-dev-server --content-base public/ --history-api-fallback --host 0.0.0.0",
99
"start:prod": "webpack && node server.js"
1010
},
1111
"author": "",

lessons/13-server-rendering/public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<html>
33
<meta charset=utf-8/>
44
<title>My First React Router App</title>
5-
<link rel=stylesheet href=/index.css>
5+
<link rel=stylesheet href=/sampleapp/index.css>
66
<div id=app></div>
7-
<script src="/bundle.js"></script>
7+
<script src="/sampleapp/bundle.js"></script>

lessons/13-server-rendering/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var express = require('express')
22
var path = require('path')
33
var compression = require('compression')
44

5+
var root = '/sampleapp'
6+
57
var app = express()
68

79
app.use(compression())
@@ -10,11 +12,11 @@ app.use(compression())
1012
app.use(express.static(path.join(__dirname, 'public')))
1113

1214
// send all requests to index.html so browserHistory works
13-
app.get('*', function (req, res) {
15+
app.get(root + '/*', function (req, res) {
1416
res.sendFile(path.join(__dirname, 'public', 'index.html'))
1517
})
1618

1719
var PORT = process.env.PORT || 8080
1820
app.listen(PORT, function() {
19-
console.log('Production Express server running at localhost:' + PORT)
21+
console.log('Production Express server running at 0.0.0.0:' + PORT)
2022
})

lessons/13-server-rendering/webpack.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
var webpack = require('webpack')
22

3+
var root = '/sampleapp'
4+
35
module.exports = {
46
entry: './index.js',
57

68
output: {
7-
path: 'public',
9+
path: 'public' + root,
810
filename: 'bundle.js',
9-
publicPath: '/'
11+
publicPath: root + '/'
1012
},
1113

1214
plugins: process.env.NODE_ENV === 'production' ? [

0 commit comments

Comments
 (0)