forked from patternfly/patternfly-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprerender.js
More file actions
59 lines (55 loc) · 1.59 KB
/
Copy pathprerender.js
File metadata and controls
59 lines (55 loc) · 1.59 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
57
58
59
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const { ServerLocation } = require('@reach/router');
const ssrPrepass = require('react-ssr-prepass');
// react, react-dom, and @reach/router are all EXCLUDED from the ssr-bundle.js
// The versions imported above are used instead, which allows us to use <ServerLocation>
// const ssrPrepass = require('react-ssr-prepass');
// This function is effectively synchronous because it mutates global.setTimeout
// Only allow one copy at a time to run
async function prerender(url) {
const location = { pathname: url };
// For @reach/router
global.history = {};
global.location = location
global.window = {
location,
// For virtualized-edit-extension
getBoundingClientRect: () => ({
height: 0,
width: 0
})
};
// For monaco
global.navigator = {
userAgent: '',
maxTouchPoints: 1
};
global.document = {
queryCommandSupported: () => true,
// For react-consoles
createElementNS: () => ({
download: {}
}),
documentElement: { style: {} }
};
global.self = {
// For react-console
document: global.document
};
console.log('prender', url);
const { App } = require(`${process.cwd()}/.cache/ssr-build/main`);
const WrappedApp = React.createElement(ServerLocation, { url },
React.createElement(App)
);
await ssrPrepass(WrappedApp, element => {
if (element.type.name === 'AsyncComponent') {
return element.type.preload();
}
});
const string = ReactDOMServer.renderToString(WrappedApp);
return string;
}
module.exports = {
prerender
};