-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathprerender.js
55 lines (47 loc) · 1.34 KB
/
prerender.js
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
import { h, options, cloneElement } from 'preact';
import renderToString from 'preact-render-to-string';
let vnodeHook;
const old = options.vnode;
options.vnode = vnode => {
if (old) old(vnode);
if (vnodeHook) vnodeHook(vnode);
};
/**
* @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
* @param {object} [options]
* @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
* @param {object} [options.props] Additional props to merge into the root JSX element
*/
export default async function prerender(vnode, options) {
options = options || {};
const maxDepth = options.maxDepth || 10;
const props = options.props;
let tries = 0;
if (typeof vnode === 'function') {
vnode = h(vnode, props);
} else if (props) {
vnode = cloneElement(vnode, props);
}
const render = () => {
if (++tries > maxDepth) return;
try {
return renderToString(vnode);
} catch (e) {
if (e && e.then) return e.then(render);
throw e;
}
};
let links = new Set();
vnodeHook = ({ type, props }) => {
if (type === 'a' && props && props.href && (!props.target || props.target === '_self')) {
links.add(props.href);
}
};
try {
let html = await render();
html += `<script type="isodata"></script>`;
return { html, links };
} finally {
vnodeHook = null;
}
}