1- var s3 = new ( require ( 'aws-sdk' ) ) . S3 ( { params :{ Bucket : process . env . S3_BUCKET_NAME } } ) ;
1+ var s3 = new ( require ( 'aws-sdk' ) ) . S3 ( { params : { Bucket : process . env . S3_BUCKET_NAME } } ) ;
22
33module . exports = {
44
5- requestReceived : function ( req , res , next ) {
6- if ( req . method !== 'GET' ) {
5+ requestReceived : function ( req , res , next ) {
6+ if ( ! [ 'HEAD' , 'GET' ] . includes ( req . method ) ) {
77 return next ( ) ;
88 }
99
@@ -14,24 +14,40 @@ module.exports = {
1414 }
1515
1616 s3 . getObject ( {
17- Key : key
17+ Key : key
1818 } , function ( err , result ) {
1919
2020 if ( ! err && result ) {
21+ var ifModifiedSince = new Date ( req . headers [ 'if-modified-since' ] ) ;
22+ var lastModified = new Date ( result [ 'LastModified' ] ) ;
23+ var now = Date . now ( ) ;
24+
25+ if ( 'LastModified' in result ) {
26+ res . setHeader ( 'Last-Modified' , lastModified . toUTCString ( ) ) ;
27+ }
28+
29+ if ( ifModifiedSince &&
30+ lastModified &&
31+ ifModifiedSince < now &&
32+ lastModified < ifModifiedSince ) {
33+
34+ return res . send ( 304 ) ;
35+ }
36+
2137 return res . send ( 200 , result . Body ) ;
2238 }
2339
2440 next ( ) ;
2541 } ) ;
2642 } ,
2743
28- pageLoaded : function ( req , res , next ) {
29- if ( req . prerender . statusCode !== 200 ) {
44+ pageLoaded : function ( req , res , next ) {
45+ if ( req . prerender . statusCode !== 200 ) {
3046 return next ( ) ;
3147 }
3248
3349 var key = req . prerender . url ;
34-
50+
3551 if ( process . env . S3_PREFIX_KEY ) {
3652 key = process . env . S3_PREFIX_KEY + '/' + key ;
3753 }
@@ -41,7 +57,7 @@ module.exports = {
4157 ContentType : 'text/html;charset=UTF-8' ,
4258 StorageClass : 'REDUCED_REDUNDANCY' ,
4359 Body : req . prerender . content
44- } , function ( err , result ) {
60+ } , function ( err , result ) {
4561
4662 if ( err ) console . error ( err ) ;
4763
0 commit comments