Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b55983

Browse files
committed
feat: allow SSR to be disabled
1 parent 9bd943b commit 3b55983

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

‎config/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const { mapValues, keyBy } = require('lodash');
22

33
module.exports = {
4+
// Enable or disable server-side rendering
5+
enableSSR: true,
6+
47
// Enable or disable dynamic imports (code splitting)
58
enableDynamicImports: false,
69

‎nodemon.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"watch": [
3+
"config/**/*.js",
34
"server/**/*.js",
45
"server/**/*.json",
5-
"server/templates/**/*.html",
6-
"common/js/**/*.js"
6+
"server/templates/**/*.html"
77
]
88
}

‎server/renderer/handler.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export default function handleRender(req, res) {
3232
// Grab the initial state from our Redux store
3333
const finalState = store.getState();
3434

35+
// If SSR is disabled, just render the skeleton HTML.
36+
if (!config.enableSSR) {
37+
const markup = render(null, finalState, []);
38+
return res.send(markup);
39+
}
40+
3541
// See react-router's Server Rendering section:
3642
// https://reacttraining.com/react-router/web/guides/server-rendering
3743
const matchRoutes = routes => {
@@ -88,20 +94,30 @@ export default function handleRender(req, res) {
8894

8995
let context = {}, modules = [];
9096

91-
const component = (
92-
<Loadable.Capturereport={moduleName=>modules.push(moduleName)}>
97+
const getComponent = ()=>{
98+
letcomponent=(
9399
<Provider store={store}>
94100
<StaticRouter context={context} location={req.baseUrl}>
95101
<App />
96102
</StaticRouter>
97103
</Provider>
98-
</Loadable.Capture>
99-
);
104+
);
105+
106+
if (!config.enableDynamicImports) {
107+
return component;
108+
}
109+
110+
return (
111+
<Loadable.Capture report={moduleName => modules.push(moduleName)}>
112+
{component}
113+
</Loadable.Capture>
114+
);
115+
};
100116

101117
// Execute the render only after all promises have been resolved.
102118
Promise.all(fetchData).then(() => {
103119
const state = store.getState();
104-
const html = renderToString(component);
120+
const html = renderToString(getComponent());
105121
const bundles = stats && getBundles(stats, modules) || [];
106122
const markup = render(html, state, bundles);
107123

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /