-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexecute-route-module.js
61 lines (51 loc) · 1.38 KB
/
execute-route-module.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
56
57
58
59
60
61
import { renderToString, renderFromHTML } from "wc-compiler";
async function executeRouteModule({
moduleUrl,
compilation,
page = {},
prerender = false,
htmlContents = null,
scripts = [],
request,
}) {
const data = {
layout: null,
body: null,
frontmatter: null,
html: null,
};
if (prerender) {
const scriptURLs = scripts.map((scriptFile) => new URL(scriptFile));
const { html } = await renderFromHTML(htmlContents, scriptURLs);
data.html = html;
} else {
const module = await import(moduleUrl).then((module) => module);
const {
prerender = false,
getLayout = null,
getBody = null,
getFrontmatter = null,
isolation,
} = module;
if (module.default) {
const { html } = await renderToString(new URL(moduleUrl), false, request);
data.body = html;
} else {
if (getBody) {
data.body = await getBody(compilation, page, request);
}
}
if (getLayout) {
data.layout = await getLayout(compilation, page);
}
if (getFrontmatter) {
data.frontmatter = await getFrontmatter(compilation, page);
}
// TODO cant we get these from just pulling from the file during the graph phase?
// https://github.com/ProjectEvergreen/greenwood/issues/991
data.prerender = prerender;
data.isolation = isolation;
}
return data;
}
export { executeRouteModule };