sladg / nextjs-lambda

Lambda deployments for Nextjs12 & Nextjs13 (standalone). Easy CLI commands to get your standalone Next output to run in AWS Lambda (not @Edge)! Uses CDK in behind and produces code zips importable to Terraform, Serverless, Azure, etc.
MIT License
169 stars 19 forks source link

fix: set cache control on lambda response #60

Closed khuezy closed 1 year ago

khuezy commented 1 year ago

Cache Control headers are currently set at the internal res level, the lambda itself needs to pass the headers back to CF/Browser.

sladg commented 1 year ago

Caching happens on Cloudfront. There is no need for S3, Lambda or ApiGw to return Cache-Control information back as we are using cache policy for this route.

See

        const assetsCachePolicy = new CachePolicy(this, 'NextPublicCachePolicy', {
            queryStringBehavior: CacheQueryStringBehavior.all(),
            enableAcceptEncodingGzip: true,
            defaultTtl: Duration.hours(12),
        })
khuezy commented 1 year ago

Make sense, but what happens if you set a different cache control data on individual items in the bucket? Will it respect those cache-controls, or will it just use the assetsCachePolicy?

The image optimization reads the cache control from the bucket:

if (response.CacheControl) {
   res.setHeader('Cache-Control', response.CacheControl)
}
sladg commented 1 year ago

Bucket's cache-control is ignored. I cannot really see use of setting cache-control for individual items, could you provide some business scenario when this might be needed? I'm striving for simple out-of-the-box solution, not to cover all edge cases.