This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
prerender.js
68 lines (61 loc) · 1.9 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
56
57
58
59
60
61
62
63
64
65
66
67
68
const nightmare = require('nightmare')({
switches: {
'ignore-certificate-errors': true
},
show: true
});
const fs = require('fs');
const axios = require('axios');
const path = require('path');
const configPath = './build/wyvernConfig.json';
function mkpathIfNotExists(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
mkpathIfNotExists(dirname);
fs.mkdirSync(dirname);
}
// Check config
if (!fs.existsSync(configPath)) {
throw new Error(`Wyvern is not configured - check your ${configPath} or use npm run dev to configure via CLI`);
}
// Read config
const config = require(configPath);
// Get config and available routes
axios.get(`${config.root}/wyvern/v1/config`)
.then((response) => {
response.data.routes.reduce((accumulator, route) => {
return accumulator.then((results) => {
const url = `${config.base_url}${route.path.replace(config.base_url, '')}`;
const relative = url.replace(config.base_url, '');
return nightmare
.goto(url)
.wait(5000)
.evaluate(() =>
document.querySelector('#app').innerHTML
)
.then((content) => {
// Push result to results array
results.push({
relative,
content
});
// Write file with contents
const filepath = `prerender${relative}index.html`;
mkpathIfNotExists(filepath);
fs.writeFileSync(filepath, content);
// Return results
return results;
})
.catch((exception) => {
console.log(exception);
});
});
}, Promise.resolve([])).then((results) => {
console.log(`PATHS PRERENDERED: ${results.length}`);
return nightmare.end();
});
})
.catch((error) => {
});