Amazon Cloudfront Cache-Control: no-cache header has no effect after 24 hours

后端 未结 2 1474
猫巷女王i
猫巷女王i 2021-01-30 17:04

I\'m hosting a static website in S3 and using Cloudfront to cache files. I\'ve essentially got 3 files with the following headers:

  • index.html (Cache-Control: no-ca
2条回答
  •  醉酒成梦
    2021-01-30 17:51

    This is more of a comment, but a bit too long. Hopefully helps others that land here.

    Cache busting via query parameter has drawbacks, although perhaps you can combat them all via Cloudfront behaviors. See https://stackoverflow.com/a/24166106/630614. Still, I would recommend unique filenames e.g. app.css?v=14113e2c764 becomes app.14113e2c764.css.

    To respond to BradLaney's comment/issue: If you've updated cache-control headers and don't see the changes, it's because the origin item is already cached – invalidate it and you should see the new headers the next time you view the resource.

    Regarding race condition when setting cache-control for S3 items, or just setting cache-control in general for an SPA, this is what's working well for my team:

    # Sync all files with 1 week cache-control, excluding .html files.
    aws s3 sync --cache-control 'max-age=604800' --exclude *.html dist/ s3://$AWS_BUCKET/
    # Sync remaining .html files with no cache.
    aws s3 sync --cache-control 'no-cache' dist/ s3://$AWS_BUCKET/
    

提交回复
热议问题