Skip to content

WIP Add env var to disable SSR #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/components/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ function Html({ css, js, html, head, initialState }) {
<div id="root" dangerouslySetInnerHTML={{
__html: html
}} />
<script dangerouslySetInnerHTML={{
__html: `window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}`
}} />
{initialState ? (
<script dangerouslySetInnerHTML={{
__html: `window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}`
}} />
) : null}
<script src={js}></script>
</body>
</html>
Expand All @@ -35,7 +37,7 @@ Html.propTypes = {
js: PropTypes.string.isRequired,
html: PropTypes.string,
head: PropTypes.object.isRequired,
initialState: PropTypes.object.isRequired
initialState: PropTypes.object
};

export default Html;
3 changes: 2 additions & 1 deletion src/env/development.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
// Env files support comments
"CLIENT_ENV": "development",
"API_ENDPOINT": "http://localhost:6060/api"
"API_ENDPOINT": "http://localhost:6060/api",
"DISABLE_UNIVERSAL_RENDERING": true
}
63 changes: 46 additions & 17 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import React from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { Provider } from 'react-redux';
import Helmet from 'react-helmet';
import configureStore from './store/configureStore';
import routes, { NotFoundComponent } from './routes';
import Html from './components/html';

let match;
let RouterContext;
let Provider;
let configureStore;
let routes;
let NotFoundComponent;

if (!process.env.DISABLE_UNIVERSAL_RENDERING) {
match = require('react-router').match;
RouterContext = require('react-router').RouterContext;
Provider = require('react-redux').Provider;
configureStore = require('./store/configureStore').default;
routes = require('./routes').default;
NotFoundComponent = require('./routes').NotFoundComponent;
}

const DOCTYPE = '<!doctype html>';

function fetchComponentData(renderProps, store) {
const requests = renderProps.components
// filter undefined values
Expand Down Expand Up @@ -45,43 +59,57 @@ function getCssFromStats(stats) {
return stats.assetsByChunkName.client.find(asset => /\.css$/.test(asset));
}

function render(stats, renderProps, store) {
const js = getJsFromStats(stats);
const css = getCssFromStats(stats);

const markup = renderToString(
function renderApp(renderProps, store) {
const html = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);

const state = store.getState();

return [html, state];
}

function render(stats, html = '', state) {
const js = getJsFromStats(stats);
const css = getCssFromStats(stats);
const head = Helmet.rewind();

const html = renderToStaticMarkup(
return renderToStaticMarkup(
<Html
js={js && `/${js}`}
css={css && `/${css}`}
html={markup}
html={html}
head={head}
initialState={store.getState()} />
initialState={state} />
);

return html;
}

/**
* Express middleware to render HTML using react-router
* @param {object} stats Webpack stats output
* @return {function} middleware function
*/
export default stats => {
export default (stats) => {

/**
* @param {object} req Express request object
* @param {object} res Express response object
* @return {undefined} undefined
*/
return (req, res, next) => {
if (process.env.DISABLE_UNIVERSAL_RENDERING) {
let html;
try {
html = render(stats);
} catch (ex) {
return next(ex);
}
return res.status(200)
.send(`${DOCTYPE}${html}`);
}

match({
routes,
location: req.url
Expand All @@ -96,12 +124,13 @@ export default stats => {
.then(() => {
let html;
try {
html = render(stats, renderProps, store);
const [markup, state] = renderApp(renderProps, store);
html = render(stats, markup, state);
} catch (ex) {
return next(ex);
}
res.status(isNotFound(renderProps) ? 404 : 200)
.send(`<!doctype html>${html}`);
.send(`${DOCTYPE}${html}`);
});
}
});
Expand Down