Last active
February 22, 2019 12:36
-
-
Save adamhenson/40b90154d2cf4e55c081f92c8dfe8877 to your computer and use it in GitHub Desktop.
An example server side render with Loadable Components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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