-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (26 loc) · 743 Bytes
/
index.js
File metadata and controls
30 lines (26 loc) · 743 Bytes
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
var Cache = require('file-system-cache');
const ttl = process.env.CACHE_PAGE_TTL || 86400000;
module.exports = {
init: function() {
this.cache = Cache.default();
},
requestReceived: function(req, res, next) {
this.cache.get(req.prerender.url).then(function (result) {
if (result && Date.now() - result.time < ttl) {
req.prerender.cacheHit = true;
res.send(200, result.content);
} else {
next();
}
}).catch((error) => console.error(error));
},
beforeSend: function(req, res, next) {
if (!req.prerender.cacheHit && req.prerender.statusCode == 200) {
this.cache.set(req.prerender.url, {
time: Date.now(),
content: req.prerender.content,
});
}
next();
}
};