Repository navigation
Expand file tree
/
Copy pathprerender.mjs
More file actions
82 lines (69 loc) · 1.87 KB
/
Copy pathprerender.mjs
File metadata and controls
82 lines (69 loc) · 1.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { readFile, writeFile, mkdir } from 'fs/promises';
import SimpleDOM from 'simple-dom/dist/commonjs/es5/index.js';
import { join } from 'path';
import Module from 'node:module';
import { globSync } from 'glob';
const require = Module.createRequire(import.meta.url);
global.FastBoot = {
require(thing) {
require(thing);
},
};
import Result from './result.mjs';
const { default: App } = await import('./dist-ssr/app.mjs');
const wrapperHTML = await readFile('./dist/index.html', 'utf8');
let instance = App.create({
autoboot: false,
modulePrefix: 'rfcs-app',
});
async function preRender(path) {
console.log(`Rendering ${path}`);
try {
const result = await render(path, instance);
result._finalizeHTML();
await mkdir(join('dist', path), { recursive: true });
await writeFile(join('dist', path, 'index.html'), await result.html());
} catch (e) {
console.error(`Error Rendering path: ${e.message}`);
}
}
function buildBootOptions() {
let doc = new SimpleDOM.Document();
let rootElement = doc.body;
return {
isBrowser: false,
document: doc,
rootElement,
shouldRender: true,
};
}
async function render(url, instance) {
let bootOptions = buildBootOptions();
await instance.visit(url, bootOptions);
return new Result(bootOptions.document, wrapperHTML, {});
}
const routesToPrerender = [
'/',
'/rfcs-input/',
'/rfcs-help/',
'/stages/',
'/create-rfc/',
'/role-core-team/',
'/stages/proposed/',
'/stages/exploring/',
'/stages/accepted/',
'/stages/ready-for-release/',
'/stages/released/',
'/stages/recommended/',
'/stages/discontinued/',
'/stages/closed/',
];
const markdowns = globSync('./rfcs/text/*.md');
for (let file of markdowns) {
routesToPrerender.push(
`/id/${file.replace(/^rfcs\/text\//, '').replace(/\.md$/, '')}`,
);
}
for (let path of routesToPrerender) {
await preRender(path);
}