From 9ca408d385b3f8441c6c41b61debcfbc18d5b43b Mon Sep 17 00:00:00 2001 From: totty90 Date: Mon, 5 Sep 2016 19:29:07 +0100 Subject: [PATCH] Enables to toggle server side rendering when running the server. Configuration has a new key named `serverSideRendering` that can be toggled between `true` and `false`. Related to: #642, #634 --- src/config.js | 1 + src/server.js | 43 +++++++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/config.js b/src/config.js index 5bf4a39e6..df97bffdb 100644 --- a/src/config.js +++ b/src/config.js @@ -11,6 +11,7 @@ export const port = process.env.PORT || 3000; export const host = process.env.WEBSITE_HOSTNAME || `localhost:${port}`; +export const serverSideRendering = true; export const databaseUrl = process.env.DATABASE_URL || 'sqlite:database.sqlite'; diff --git a/src/server.js b/src/server.js index e61fd1c3d..551823cf0 100644 --- a/src/server.js +++ b/src/server.js @@ -27,7 +27,7 @@ import models from './data/models'; import schema from './data/schema'; import routes from './routes'; import assets from './assets'; // eslint-disable-line import/no-unresolved -import { port, auth } from './config'; +import { port, auth, serverSideRendering } from './config'; const app = express(); @@ -84,31 +84,30 @@ app.use('/graphql', expressGraphQL(req => ({ // ----------------------------------------------------------------------------- app.get('*', async (req, res, next) => { try { - let css = new Set(); let statusCode = 200; const data = { title: '', description: '', style: '', script: assets.main.js, children: '' }; - - await UniversalRouter.resolve(routes, { - path: req.path, - query: req.query, - context: { - insertCss: (...styles) => { - styles.forEach(style => css.add(style._getCss())); // eslint-disable-line no-underscore-dangle, max-len + if (serverSideRendering) { + let css = new Set(); + await UniversalRouter.resolve(routes, { + path: req.path, + query: req.query, + context: { + insertCss: (...styles) => { + styles.forEach(style => css.add(style._getCss())); // eslint-disable-line no-underscore-dangle, max-len + }, + setTitle: value => (data.title = value), + setMeta: (key, value) => (data[key] = value), }, - setTitle: value => (data.title = value), - setMeta: (key, value) => (data[key] = value), - }, - render(component, status = 200) { - css = new Set(); - statusCode = status; - data.children = ReactDOM.renderToString(component); - data.style = [...css].join(''); - return true; - }, - }); - + render(component, status = 200) { + css = new Set(); + statusCode = status; + data.children = ReactDOM.renderToString(component); + data.style = [...css].join(''); + return true; + }, + }); + } const html = ReactDOM.renderToStaticMarkup(); - res.status(statusCode); res.send(`${html}`); } catch (err) {