Skip to content

Commit 565abd8

Browse files
committed
Part 2: Enabling SSR
1 parent 2e2619a commit 565abd8

11 files changed

Lines changed: 1260 additions & 38 deletions

File tree

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"start": "react-scripts start",
1717
"build": "react-scripts build && npm run move-build-to-server",
18-
"move-build-to-server": "mv build _build && mv _build ../server && cd ../server && rm -rf public && mv _build public",
18+
"move-build-to-server": "mv build _build && mv _build ../server && cd ../server && rm -rf public && mv _build public && mv public/index.html public/layout.html",
1919
"test": "react-scripts test --env=jsdom",
2020
"eject": "react-scripts eject"
2121
},

client/src/App.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ export class AppComponent extends Component {
1818
}
1919

2020
componentDidMount () {
21-
this.props.fetchPosts()
21+
const { inited, fetchPosts } = this.props
22+
if (!inited) { fetchPosts() }
2223
}
2324
}
2425

2526
export const App = connect(
26-
state => ({ posts: state.posts }),
27+
state => ({
28+
posts: state.posts,
29+
inited: state.inited
30+
}),
2731
dispatch => bindActionCreators(actionCreators, dispatch)
2832
)(AppComponent)
2933

client/src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import 'isomorphic-fetch'
33
import ReactDOM from 'react-dom'
44
import './index.css'
55
import App from './App'
6-
import registerServiceWorker from './registerServiceWorker'
6+
import { unregister } from './registerServiceWorker'
77
import thunkMiddleware from 'redux-thunk'
88
import { Provider } from 'react-redux'
99
import { createStore, applyMiddleware } from 'redux'
1010
import { root } from './reducers'
1111

12-
const store = createStore(root, applyMiddleware(thunkMiddleware))
12+
const initState = window.__INIT_STATE__ && JSON.parse(window.__INIT_STATE__)
13+
14+
const store = createStore(root, initState, applyMiddleware(thunkMiddleware))
1315

1416
ReactDOM.render(
1517
<Provider store={store}>
@@ -18,4 +20,4 @@ ReactDOM.render(
1820
document.getElementById('root')
1921
)
2022

21-
registerServiceWorker()
23+
unregister()

client/src/reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { combineReducers } from 'redux'
22
import { posts } from './posts'
3+
import { inited } from './inited'
34

45
export const root = combineReducers({
5-
posts
6+
posts,
7+
inited
68
})

client/src/reducers/inited.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { RECEIVE_POSTS, ERROR_POSTS } from '../actions'
2+
3+
export function inited (state = false, action) {
4+
switch (action.type) {
5+
case RECEIVE_POSTS:
6+
case ERROR_POSTS:
7+
return true
8+
default:
9+
return state
10+
}
11+
}

client/src/renderToStrings.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import { createStore } from 'redux'
3+
import { root } from './reducers'
4+
import { receivePosts, errorPosts } from './actions'
5+
import { Provider } from 'react-redux'
6+
import { renderToString } from 'react-dom/server'
7+
import { App } from './App'
8+
9+
export function renderToStrings (posts) {
10+
const store = createStore(root)
11+
store.dispatch(
12+
posts ? receivePosts(posts) : errorPosts()
13+
)
14+
15+
const html = renderToString(
16+
<Provider store={store}>
17+
<App />
18+
</Provider>
19+
)
20+
const state = JSON.stringify(JSON.stringify(store.getState()))
21+
22+
return { html, state }
23+
}

0 commit comments

Comments
 (0)