-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
51 lines (38 loc) · 930 Bytes
/
index.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
var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}});
module.exports = {
requestReceived: function(req, res, next) {
if(req.method !== 'GET') {
return next();
}
var key = req.prerender.url;
if (process.env.S3_PREFIX_KEY) {
key = process.env.S3_PREFIX_KEY + '/' + key;
}
s3.getObject({
Key: key
}, function (err, result) {
if (!err && result) {
return res.send(200, result.Body);
}
next();
});
},
pageLoaded: function(req, res, next) {
if(req.prerender.statusCode !== 200) {
return next();
}
var key = req.prerender.url;
if (process.env.S3_PREFIX_KEY) {
key = process.env.S3_PREFIX_KEY + '/' + key;
}
s3.putObject({
Key: key,
ContentType: 'text/html;charset=UTF-8',
StorageClass: 'REDUCED_REDUNDANCY',
Body: req.prerender.content
}, function(err, result) {
if (err) console.error(err);
next();
});
}
};