import * as ReactDOMServer from 'react-dom/server'; import cookie from 'cookie'; // import our main App component import App from '../../src/App'; import { getLinks } from '../sape'; import { StaticRouter } from 'react-router-dom/server'; const path = require('path'); const fs = require('fs'); const serverRenderer = async (req, res, next) => { // point to the html file created by CRA's build tool const filePath = path.resolve(__dirname, '..', '..', 'dist', 'index.html'); // links const cookies = cookie.parse(req.headers.cookie || ''); const links = await getLinks(req.originalUrl, cookies['sape_cookie']); fs.readFile(filePath, 'utf8', (err, htmlData) => { if (err) { console.error('err', err); return res.status(404).end(); } const routerContext = {}; const props = { footer: links.join(' ') }; const marker = '
'; const data = htmlData.split(marker); const propsData = `${marker}`; const { pipe } = ReactDOMServer.renderToPipeableStream( , { onShellReady() { pipe(res, { end: false }); }, onAllReady() { res.write(data[1]); res.end(); } }); res.write(data[0]); res.write(propsData); }); }; export default serverRenderer;