mirror of
https://github.com/gregberge/loadable-components.git
synced 2026-01-18 15:12:26 +00:00
This issue fixes #59 It is possible to create a configuration which will result into a wrong state but it should not happen in 99.99% cases.
30 lines
742 B
JavaScript
30 lines
742 B
JavaScript
import path from 'path'
|
|
import React from 'react'
|
|
import ReactDOMServer from 'react-dom/server'
|
|
import express from 'express'
|
|
import { getLoadableState } from 'loadable-components/server'
|
|
import App from './App'
|
|
|
|
const app = express()
|
|
|
|
app.get('/', async (req, res) => {
|
|
try {
|
|
const reactApp = <App />
|
|
const loadableState = await getLoadableState(reactApp)
|
|
const html = ReactDOMServer.renderToString(reactApp)
|
|
res.send(`<!DOCTYPE html>
|
|
<html>
|
|
<body>
|
|
<div id="root">${html}</div>
|
|
${loadableState.getScriptTag()}
|
|
<script src="bundle.js"></script>
|
|
</body>
|
|
</html>`)
|
|
} catch (err) {
|
|
res.status(500)
|
|
res.send(err.stack)
|
|
}
|
|
})
|
|
|
|
app.listen(3000, () => console.log('http://localhost:3000'))
|