-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprerender.js
125 lines (99 loc) · 4.1 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const BrowserRunner = require('../lib/browser');
const fs = require('fs');
const path = require('path');
async function optimizePage(compilation, contents, route, outputDir) {
const outputPath = `${outputDir}${route}index.html`;
const optimizeResources = compilation.config.plugins.filter((plugin) => {
return plugin.type === 'resource';
}).map((plugin) => {
return plugin.provider(compilation);
}).filter((provider) => {
return provider.shouldOptimize && provider.optimize;
});
const htmlOptimized = await optimizeResources.reduce(async (htmlPromise, resource) => {
const html = await htmlPromise;
const shouldOptimize = await resource.shouldOptimize(outputPath, html);
return shouldOptimize
? resource.optimize(outputPath, html)
: Promise.resolve(html);
}, Promise.resolve(contents));
if (!fs.existsSync(path.join(outputDir, route))) {
fs.mkdirSync(path.join(outputDir, route), {
recursive: true
});
}
await fs.promises.writeFile(outputPath, htmlOptimized);
}
async function preRenderCompilation(compilation) {
const browserRunner = new BrowserRunner();
const runBrowser = async (serverUrl, pages, outputDir) => {
try {
return Promise.all(pages.map(async(page) => {
const { route } = page;
console.info('prerendering page...', route);
return await browserRunner
.serialize(`${serverUrl}${route}`)
.then(async (indexHtml) => {
console.info(`prerendering complete for page ${route}.`);
await optimizePage(compilation, indexHtml, route, outputDir);
});
}));
} catch (e) {
// eslint-disable-next-line no-console
console.error(err);
return false;
}
};
// gracefully handle if puppeteer is not installed correctly
// like may happen in a stackblitz environment and just reject early
// otherwise we can feel confident attempating to prerender all pages
// https://github.com/ProjectEvergreen/greenwood/discussions/639
try {
await browserRunner.init();
} catch (e) {
console.error(e);
console.error('*******************************************************************');
console.error('*******************************************************************');
console.error('There was an error trying to initialize puppeteer for pre-rendering.');
console.info('To troubleshoot, please check your environment for any npm install or postinstall errors, as may be the case in a Stackblitz or other sandbox like environment.');
console.info('For more information please see this guide - https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md');
return Promise.reject();
}
return new Promise(async (resolve, reject) => {
try {
const pages = compilation.graph;
const port = compilation.config.devServer.port;
const outputDir = compilation.context.scratchDir;
const serverAddress = `http://127.0.0.1:${port}`;
console.info(`Prerendering pages at ${serverAddress}`);
console.debug('pages to render', `\n ${pages.map(page => page.path).join('\n ')}`);
await runBrowser(serverAddress, pages, outputDir);
console.info('done prerendering all pages');
browserRunner.close();
resolve();
} catch (err) {
reject(err);
}
});
}
async function staticRenderCompilation(compilation) {
const pages = compilation.graph;
const scratchDir = compilation.context.scratchDir;
const htmlResource = compilation.config.plugins.filter((plugin) => {
return plugin.name === 'plugin-standard-html';
}).map((plugin) => {
return plugin.provider(compilation);
})[0];
console.info('pages to generate', `\n ${pages.map(page => page.path).join('\n ')}`);
await Promise.all(pages.map(async (page) => {
const route = page.route;
const response = await htmlResource.serve(route);
await optimizePage(compilation, response.body, route, scratchDir);
return Promise.resolve();
}));
console.info('success, done generating all pages!');
}
module.exports = {
preRenderCompilation,
staticRenderCompilation
};