From 120c81849b743c9c9bfc4d681fdf8a37f7007a9c Mon Sep 17 00:00:00 2001 From: Owen Stowe Date: Thu, 27 May 2021 13:43:17 -0400 Subject: [PATCH] fix(ssr): disableSSR option --- packages/core/client/index.js | 11 +++++++++-- packages/core/server/utils/getTemplateVars.js | 10 ++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/core/client/index.js b/packages/core/client/index.js index d3f1df4f75..35d7d85baa 100644 --- a/packages/core/client/index.js +++ b/packages/core/client/index.js @@ -10,7 +10,10 @@ import set from 'lodash/fp/set'; import { actionLocationChange } from 'actions'; import Cookies from 'universal-cookie'; import App from 'components/app'; -import { getValueFromConfig } from 'config/irving/getValueFromConfig'; +import { + getValueFromConfig, + getValueFromUserConfig, +} from 'config/irving/getValueFromConfig'; import preloadedStateDenylist from 'config/preloadedStateDenylist'; import rootReducer from 'reducers'; import defaultState from 'reducers/defaultState'; @@ -66,12 +69,16 @@ history.listen((location, action) => { )); }); +// Determine if user wants to disable SSR. +const disableSSR = getValueFromUserConfig('disableSSR', false); +const renderMethod = disableSSR ? 'render' : 'hydrate'; + const render = () => { const rootEl = document.getElementById('root'); // It is imperative that the server React component tree matches the client // component tree, so that the client can re-hydrate the app from the server // rendered markup, otherwise the app will be completely re-rendered. - ReactDOM.hydrate( + ReactDOM[renderMethod]( , diff --git a/packages/core/server/utils/getTemplateVars.js b/packages/core/server/utils/getTemplateVars.js index 550175addf..221bfb2c60 100644 --- a/packages/core/server/utils/getTemplateVars.js +++ b/packages/core/server/utils/getTemplateVars.js @@ -2,7 +2,10 @@ import React from 'react'; import { renderToString } from 'react-dom/server'; import omit from 'lodash/fp/omit'; import mergeWith from 'lodash/mergeWith'; -import { getConfigValues } from 'config/irving/getValueFromConfig'; +import { + getConfigValues, + getValueFromUserConfig, +} from 'config/irving/getValueFromConfig'; import { getEnv, getSiteConfig } from 'config/multisite'; export const defaultHead = { @@ -19,6 +22,9 @@ export const defaultHead = { end: [], }; +// Determine if user wants to disable SSR. +const disableSSR = getValueFromUserConfig('disableSSR', false); + /** * Check if a value is a function and, if so, call that function to get underlying value. * @@ -151,6 +157,6 @@ export default function getTemplateVars( return { ...mergedVars, head: mergedHead, - appHtml, + appHtml: disableSSR ? '' : appHtml, }; }