-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (54 loc) · 1.78 KB
/
server.js
File metadata and controls
67 lines (54 loc) · 1.78 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
import prerender from 'prerender';
import memoryCache from 'prerender-memory-cache';
const server = prerender({
chromeLocation: '/usr/bin/chromium',
chromeFlags: [
'--no-sandbox',
'--headless=new',
'--disable-gpu',
'--remote-debugging-port=9222',
'--hide-scrollbars',
'--disable-dev-shm-usage',
'--ignore-certificate-errors',
'--allow-insecure-localhost'
]
});
process.env.CACHE_MAXSIZE = process.env.CACHE_MAXSIZE || 1000;
process.env.CACHE_TTL = process.env.CACHE_TTL || 43200;
server.use({
tabCreated: (req, res, next) => {
const tab = req.prerender.tab;
if (tab) {
tab.Console.enable();
tab.Network.enable();
tab.Console.messageAdded((params) => {
console.log('🟡 Browser log:', params.message.text);
});
tab.Network.loadingFailed((params) => {
console.log('🔴 Network Failed:', params.errorText, params.url);
});
tab.Runtime.enable();
tab.Runtime.exceptionThrown((exception) => {
console.log('💥 JS Exception:', exception.exceptionDetails.text);
});
}
next();
}
});
server.use({
beforeSend: (req, res, next) => {
if (req.prerender.res) {
const body = req.prerender.res.body || '';
req.prerender.res.headers = {
'content-type': 'text/html; charset=utf-8',
'content-length': Buffer.byteLength(body, 'utf8'),
// 'cache-control': 'public, max-age=600'
};
}
next();
}
});
server.use(prerender.removeScriptTags());
server.use(memoryCache);
console.log('Prerender on Node 24 is starting...');
server.start();