Skip to content

Commit effb015

Browse files
committed
add basic server side rendering
1 parent 3178e59 commit effb015

File tree

9 files changed

+48842
-10
lines changed

9 files changed

+48842
-10
lines changed

gulpfile.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ const paths = {
1313
* paths to files that should restart app
1414
*/
1515
server_files: [
16-
'models/**/*.js',
17-
'routes/**/*.js',
16+
'models/**/*',
17+
'routes/**/*',
1818
'keystone.js',
19-
'package.json'
19+
'package.json',
20+
'!routes/redux-render/**/*.js'
2021
],
2122
/**
2223
* gulp-sass configuration
@@ -36,7 +37,15 @@ const paths = {
3637
'public/js/src/**/*',
3738
'webpack.config.js'
3839
]
39-
}
40+
},
41+
'webpack-server': {
42+
config: 'webpack-server.config.js',
43+
watch_files: [
44+
'public/js/src/**/*',
45+
'routes/redux-render/index.jsx',
46+
'webpack-server.config.js'
47+
]
48+
},
4049
};
4150

4251
/** starting and restarting keystone app **/
@@ -110,6 +119,27 @@ gulp.task('webpack', done => {
110119
return webpack_process;
111120
});
112121

122+
/** webpack-server **/
123+
gulp.task('watch-webpack-server', () => {
124+
gulp.watch(paths['webpack-server'].watch_files, ['webpack-server']);
125+
});
126+
gulp.task('webpack-server', done => {
127+
var webpack_process = spawn('./node_modules/.bin/webpack', [
128+
'--config',
129+
paths['webpack-server'].config
130+
]);
131+
webpack_process.on('close', (code, signal) => {
132+
done();
133+
});
134+
webpack_process.stdout.on('data', (data) => {
135+
console.log(`${data}`);
136+
});
137+
webpack_process.stderr.on('data', (data) => {
138+
console.log(`Error: ${data}`);
139+
});
140+
return webpack_process;
141+
});
142+
113143

114144
/** gulp task setup **/
115-
gulp.task('default', ['watch-app', 'watch-sass']);//, 'watch-webpack']);
145+
gulp.task('default', ['watch-app', 'watch-sass', 'watch-webpack-server']);//, 'watch-webpack']);

public/js/src/components/Admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class Admin extends React.Component {
1212
}
1313

1414
render(){
15-
if(this.props.user.is_fetching){
15+
if(Object.keys(this.props.user.user).length === 0 && this.props.user.is_fetching){
1616
return (
1717
<div className='container-fluid'>
1818
<div className='row pb-5'>

public/js/src/containers/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import Admin from './Admin'
1717

1818
import style from '../../../styles/src/site.scss'
1919

20+
// Grab the state from a global variable injected into the server-generated HTML
21+
const preloaded_state = window.__PRELOADED_STATE__
22+
23+
// Allow the passed state to be garbage-collected
24+
delete window.__PRELOADED_STATE__
25+
2026
export default class App extends React.Component {
2127
constructor(){
2228
super()
@@ -29,6 +35,7 @@ export default class App extends React.Component {
2935

3036
this.store = createStore(
3137
reducer,
38+
preloaded_state,
3239
applyMiddleware(...middleware)
3340
)
3441

public/js/src/home.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// globally import bootstrap
22
import React from 'react'
3-
import ReactDOM from 'react-dom'
3+
import { hydrate } from 'react-dom'
44
import { AppContainer } from 'react-hot-loader'
55

66
import App from './containers/App'
@@ -9,7 +9,7 @@ const home_elem = document.getElementById('Home')
99

1010
const render = Component => {
1111
const NextApp = require('./containers/App').default
12-
ReactDOM.render(
12+
hydrate(
1313
<AppContainer>
1414
<NextApp />
1515
</AppContainer>, home_elem

routes/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const routes = {
3232
api: importRoutes('./api')
3333
}
3434

35+
const reduxRender = require('./redux-render/index')
36+
3537
// Setup Route Bindings
3638
exports = module.exports = (app) => {
3739

@@ -54,6 +56,19 @@ exports = module.exports = (app) => {
5456
}))
5557
}
5658

59+
app.use('/app', async (req, res, next) => {
60+
const User = keystone.list('User')
61+
let state = {
62+
user: {
63+
user: req.user || {}
64+
},
65+
users: {
66+
items: await User.model.find({})
67+
}
68+
}
69+
reduxRender(state, req, res, next)
70+
})
71+
5772
app.get('/signin', routes.views.signin)
5873
app.get('/signout', middleware.signout, routes.views.signin)
5974
app.get('/reset-password/:hash', routes.views.reset_password)

0 commit comments

Comments
 (0)