Skip to content

Instantly share code, notes, and snippets.

@adamhenson
Last active February 22, 2019 12:36
Show Gist options
  • Save adamhenson/40b90154d2cf4e55c081f92c8dfe8877 to your computer and use it in GitHub Desktop.
Save adamhenson/40b90154d2cf4e55c081f92c8dfe8877 to your computer and use it in GitHub Desktop.
An example server side render with Loadable Components
/* global LOADABLE_STATS: true */
import React from 'react';
import { renderToString } from 'react-dom/server';
import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server';
import { Provider } from './somewhere/out/there/Provider';
import App from './somewhere/out/there/App';
const extractor = new ChunkExtractor({
statsFile: LOADABLE_STATS,
});
export default (req, res) => {
const html = renderToString(
<Provider>
<ChunkExtractorManager
extractor={extractor}
>
<App />
</ChunkExtractorManager>
</Provider>,
);
return res.send(`
<!doctype html>
<html>
<head>
${extractor.getStyleTags()}
</head>
<body>
<div id="root">
${html}
</div>
${extractor.getScriptTags()}
</body>
</html>
`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment