aws-samples / image-optimization

Simple, performant and cost efficient solution for optimizing images using Amazon CloudFront, Amazon S3 and AWS Lambda
MIT No Attribution
206 stars 123 forks source link

Cache-Control header have not been populated for transformed images #56

Closed zakdma closed 2 months ago

zakdma commented 2 months ago

I hope this will help someone like me, that are struggling with Cache-Control header has not been populated. You can see there is a TRANSFORMED_IMAGE_CACHE_TTL constant that should be used for cache control for images. It defined here https://github.com/aws-samples/image-optimization/blob/main/functions/image-processing/index.mjs#L10 It then used to put cache-control for image saved to S3 https://github.com/aws-samples/image-optimization/blob/main/functions/image-processing/index.mjs#L93 This will only add x-amz-meta-cache-control header but not Cache-Control header. And Google Page Speed will tell you that images are not cached at all. According to documentation, there is not metadata but a separate field CacheControl when you put the object to S3 https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html So you have to add it to be populated in the response. Just add this line CacheControl: TRANSFORMED_IMAGE_CACHE_TTL, above the

Metadata: {
    'cache-control': TRANSFORMED_IMAGE_CACHE_TTL,
},
achrafsouk commented 2 months ago

Thank you very much for this valuable correction. It's a miss on our side. I just made the change https://github.com/aws-samples/image-optimization/commit/fb06adbec7c26208917c52a40c8b2aa61e35ad61