Skip to content

Commit 4f45889

Browse files
committed
fix SSR
1 parent 132c5dd commit 4f45889

File tree

9 files changed

+3317
-372
lines changed

9 files changed

+3317
-372
lines changed

.babelrc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
"react-optimize"
1717
],
1818
"comments": false
19-
},
20-
"server": {
21-
"presets": [
22-
"react-optimize",
23-
"babili"
24-
],
25-
"comments": false
2619
}
2720
}
2821
}

jsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"typeAcquisition": {
3+
"include": [
4+
"react",
5+
"redux",
6+
"react-router",
7+
"immutable"
8+
]
9+
}
10+
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "React + React-Router + Redux + ImmutableJS + Bootstrap with with Server side rendering, Hot Reload and redux-devtools STARTER",
55
"main": "src/index.js",
66
"engines": {
7-
"node": "8.1.4",
7+
"node": ">=8.1.4",
88
"npm": ">=5.3.0",
99
"yarn": ">=0.27.5"
1010
},
@@ -17,8 +17,8 @@
1717
"start": "cross-env NODE_ENV=dev node server.hot.reload",
1818
"serve-spa-dev": "npm run dev && nodemon ./src/server/SPA/index.js",
1919
"serve-spa-prod": "npm run prod && node ./src/server/SPA/index.js",
20-
"build-ssr": "cross-env NODE_ENV=server webpack --color --config webpack.server.ssr.config.js",
21-
"serve-ssr": "npm run build-ssr && node ./src/server/SSR/index.js"
20+
"build-ssr": "cross-env NODE_ENV=production webpack --color --config webpack.server.ssr.config.js",
21+
"serve-ssr": "npm run build-ssr && cross-env NODE_ENV=production node ./src/server/SSR/index.js"
2222
},
2323
"keywords": [
2424
"react",
@@ -35,7 +35,7 @@
3535
"webpack",
3636
"webpack3",
3737
"hot-reload",
38-
"redux-devtools",
38+
"redux-devtools-extension",
3939
"devtools"
4040
],
4141
"repository": {

src/server/SSR/index.js

Lines changed: 3110 additions & 210 deletions
Large diffs are not rendered by default.

src/server/SSR/src/server.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ const morgan = require('morgan');
1919
import React from 'react';
2020
import { renderToString } from 'react-dom/server';
2121
import moment from 'moment';
22-
import { StaticRouter } from 'react-router';
22+
import {
23+
StaticRouter,
24+
Switch,
25+
Route
26+
} from 'react-router';
2327
import { Provider } from 'react-redux';
2428
import configureStore from '../../../app/redux/store/configureStore';
2529
import App from '../../../app/containers/app/App';
30+
import ScrollToTop from '../../../app/components/scrollToTop/ScrollToTop';
31+
import Login from '../../../app/views/login';
32+
import PageNotFound from '../../../app/views/pageNotFound/PageNotFound'; // not connected to redux (no index.js)
33+
import LogoutRoute from '../../../app/components/logoutRoute/LogoutRoute';
2634

2735

2836
const DOCS_PATH = '../../../../docs';
@@ -47,7 +55,7 @@ app.use(morgan('combined'));
4755

4856
app.use('/assets', express.static(path.resolve(__dirname, DOCS_PATH, 'public/assets/')));
4957

50-
// IMPORTANT: '/*' and not '/'
58+
// IMPORTANT: '/*' and not '/'
5159
// since you want browser refresh (= first render) to work from any route of the application
5260
app.get('/*', serverRender);
5361

@@ -104,7 +112,7 @@ async function serverRender(req, res) {
104112
const response = await fakeFetch();
105113
const { info } = response;
106114
const currentTime = moment().format();
107-
const currentState = store.getState();
115+
const currentState = store.getState();
108116

109117
const currentViewsState = currentState.get('views');
110118
const updatedViewState = currentViewsState
@@ -131,7 +139,14 @@ async function serverRender(req, res) {
131139
<StaticRouter
132140
location={location}
133141
context={context}>
134-
<App />
142+
<ScrollToTop>
143+
<Switch>
144+
<Route exact path="/login" component={Login} />
145+
<App />
146+
<LogoutRoute path="/logout" />
147+
<Route component={PageNotFound} />
148+
</Switch>
149+
</ScrollToTop>
135150
</StaticRouter>
136151
</Provider>
137152
);
@@ -163,15 +178,15 @@ async function serverRender(req, res) {
163178
// .then(
164179
// ({ info }) => {
165180
// const currentTime = moment().format();
166-
// const currentState = store.getState();
167-
181+
// const currentState = store.getState();
182+
168183
// const currentViewsState = currentState.get('views');
169184
// const updatedViewState = currentViewsState
170185
// .set('somePropFromServer', info)
171186
// .set('serverTime', currentTime);
172187

173188
// const preWarmedState = currentState.set('views', updatedViewState);
174-
189+
175190
// // //JS would be then:
176191
// // preWarmedState = {
177192
// // ...currentState,
@@ -184,7 +199,7 @@ async function serverRender(req, res) {
184199

185200
// // update store to be preloaded:
186201
// store = configureStore(preWarmedState);
187-
202+
188203
// const InitialView = (
189204
// <Provider store={store}>
190205
// <StaticRouter
@@ -228,8 +243,8 @@ function renderFullPage(html, preloadedState = '') {
228243
// </section>
229244
// will throw warning related to: https://stackoverflow.com/questions/34060968/react-warning-render
230245
//
231-
// so write this way to fix:
232-
// <section id="root">${html}</section>
246+
// so write this way to fix:
247+
// <section id="root">${html}</section>
233248
const indexHtml = {
234249
template: `
235250
<!DOCTYPE html>

webpack.dev.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const config = {
5353
},
5454
{
5555
test: /\.css$/,
56-
use: SPLIT_STYLE
56+
use: SPLIT_STYLE
5757
? ExtractTextPlugin.extract({
5858
fallback: 'style-loader',
5959
use: [
@@ -69,7 +69,7 @@ const config = {
6969
},
7070
{
7171
test: /\.scss$/,
72-
use: SPLIT_STYLE
72+
use: SPLIT_STYLE
7373
? ExtractTextPlugin.extract({
7474
fallback: 'style-loader',
7575
use: [
@@ -105,7 +105,7 @@ const config = {
105105
new ExtractTextPlugin('app.styles.css'),
106106
new webpack.optimize.CommonsChunkPlugin({
107107
name: 'vendor',
108-
filename: 'app.vendor.bundle.js'
108+
filename: 'app.vendor.bundle.js'
109109
})
110110
]
111111
};
@@ -115,8 +115,9 @@ const config = {
115115
*/
116116
function getImplicitGlobals() {
117117
return new webpack.ProvidePlugin({
118-
$: 'jquery',
119-
jQuery: 'jquery'
118+
$: 'jquery',
119+
jQuery: 'jquery',
120+
jquery: 'jquery'
120121
});
121122
}
122123

webpack.hot.reload.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ const config = {
7272
*/
7373
function getImplicitGlobals() {
7474
return new webpack.ProvidePlugin({
75-
$: 'jquery',
76-
jQuery: 'jquery'
75+
$: 'jquery',
76+
jQuery: 'jquery',
77+
jquery: 'jquery'
7778
});
7879
}
7980

0 commit comments

Comments
 (0)