Skip to content

Instantly share code, notes, and snippets.

@kellyrmilligan
Created June 8, 2017 13:38
Show Gist options
  • Save kellyrmilligan/e242d3dc743105fe91a83cc933ee1314 to your computer and use it in GitHub Desktop.
Save kellyrmilligan/e242d3dc743105fe91a83cc933ee1314 to your computer and use it in GitHub Desktop.
Sync files to s3 and set cache control headers
#!/bin/bash
if [[ "$1" != "" ]]; then
S3BUCKETNAME="$1"
else
echo ERROR: Failed to supply S3 bucket name
exit 1
fi
aws s3 sync build s3://$S3BUCKETNAME --delete --cache-control max-age=31536000,public
aws s3 cp s3://$S3BUCKETNAME/service-worker.js s3://$S3BUCKETNAME/service-worker.js --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type application/javascript --acl public-read
aws s3 cp s3://$S3BUCKETNAME/index.html s3://$S3BUCKETNAME/index.html --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type text/html --acl public-read
@kelly-tock
Copy link

Nice!

@agconti
Copy link

agconti commented Jun 8, 2020

The cache-control directive doesn't make sense here:

max-age=0,no-cache,no-store,must-revalidate
  • no-cache and no-store have different effects and are mutually exclusive, meaning the browser cannot adhere to both at the same time
  • no-cache already equals must-revalidate.
  • no-store already equals max-age=0.

I use no-cache for my html files to ensure that I never serve stale versions while minimizing my bandwidth costs.
Here's a great resource for figuring this out for yourself: https://web.dev/http-cache/#defining-optimal-cache-control-policy

Copy link

ghost commented Nov 24, 2020

thanks !

@dnsorlov
Copy link

dnsorlov commented Mar 4, 2021

@agconti

  • no-store already equals max-age=0.

no-store is not equal to max-age=0 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#preventing_caching

@slavafomin
Copy link

Thanks for the commands example, however, the Cache-Control directive in it is plainly wrong and doesn't make sense, as also mentioned by the @agconti.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment