forked from module-federation/module-federation-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
56 lines (49 loc) · 2.08 KB
/
render.js
File metadata and controls
56 lines (49 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React from "react";
import { renderToString } from "react-dom/server";
import { Helmet } from "react-helmet";
import { ChunkExtractor } from "@loadable/server";
import path from "path";
import App from "../src/components/App";
const statsFile = path.resolve("./buildClient/static/stats.json");
export default async (req, res, next) => {
try {
// This is the stats file generated by webpack loadable plugin
// We create an extractor from the statsFile
const extractor = new ChunkExtractor({ statsFile });
// Wrap your application using "collectChunks"
const jsx = extractor.collectChunks(createApp(App));
// Render your application
const html = renderToString(jsx);
// You can now collect your script tags
const scriptTags = extractor.getScriptTags(); // or extractor.getScriptElements();
// You can also collect your "preload/prefetch" links
const linkTags = extractor.getLinkTags(); // or extractor.getLinkElements();
// And you can even collect your style tags (if you use "mini-css-extract-plugin")
const styleTags = extractor.getStyleTags(); // or extractor.getStyleElements();
// console.log('extractor',jsx)
// console.log('scriptTags',scriptTags)
// const appString = renderToString(app)
const helmet = Helmet.renderStatic();
// const chunkNames = flushChunkNames()
// const { js, styles, cssHash } = flushChunks(clientStats, { chunkNames })
// res.render('send', { csrfToken: req.csrfToken() })
return res.send(`<!doctype html>
<html ${helmet.htmlAttributes.toString()}>
<head>
${helmet.title.toString()}
${helmet.meta.toString()}
${helmet.link.toString()}
<script src="http://localhost:3002/static/container.js"></script>
<link rel="shortcut icon" href="data:;base64,=">
${styleTags}
</head>
<body ${helmet.bodyAttributes.toString()}>
<div id="root">${html}</div>
${scriptTags}
</body>
</html>`);
} catch (err) {
console.error(err);
}
};
const createApp = (App) => <App />;