forked from prerender/prerender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·63 lines (54 loc) · 1.66 KB
/
server.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
#!/usr/bin/env node
const app = require('express')()
const bodyParser = require('body-parser')
const compression = require('compression')
const status = require('express-status-monitor')
const cacheManager = require('cache-manager')
const S3Cache = require('cache-manager-s3')
const PrerenderCacheManagerGlue = require('prerender-cache-manager-glue')
const port = 3000
app.disable('x-powered-by')
app.use(compression())
app.use(status())
let cachePlugin
if( !('S3_ACCESS_KEY' in process.env) || !process.env.S3_ACCESS_KEY ) {
cachePlugin = require('prerender-memory-cache')
console.log('No S3 key detected, using memory cache')
} else {
cachePlugin = new S3Cache({
accessKey: process.env.S3_ACCESS_KEY,
secretKey: process.env.S3_SECRET_KEY,
bucket: process.env.S3_BUCKET,
})
}
const prerenderCache = new PrerenderCacheManagerGlue(
cacheManager.caching({
store: cachePlugin,
ttl: 3600,
})
)
// Prerenderer
const prerender = require('prerender')({
chromeLocation: process.env.CHROME_BIN,
chromeFlags: [
'--no-sandbox',
'--remote-debugging-port=9222',
'--headless',
'--disable-gpu',
'--disable-translate',
'--disable-extensions',
'--hide-scrollbars',
],
logRequests: true,
plugins: [
require('prerender/plugins/whitelist'),
require('prerender/plugins/removeScriptTags'),
require('prerender/plugins/httpHeaders'),
require('prerender/plugins/blockResources')(),
prerenderCache,
]
}).start()
app.get('*', prerender.onRequest)
// dont check content-type and just always try to parse body as json
app.post('*', bodyParser.json({ type: () => true }), prerender.onRequest)
app.listen(port, () => console.log(`Server accepting requests on port ${port}`))