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
47 lines (40 loc) · 1.6 KB
/
render.js
File metadata and controls
47 lines (40 loc) · 1.6 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
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 {
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();
const helmet = Helmet.renderStatic();
return res.send(`<!doctype html>
<html ${helmet.htmlAttributes.toString()}>
<head>
${helmet.title.toString()}
${helmet.meta.toString()}
${helmet.link.toString()}
<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 />;