Static Site on Google Cloud Storage: Caching

Static Site on Google Cloud Storage: Caching
Page content

Be aware Google Cloud storage sets Cache-Control:public, max-age=3600 header for all files by default. This means files are going to be cached by browsers and proxies so it is going to take up to an hour for folks on the Internet to see an update.

I’ve decided to decrease default max-age value to 600. So I upload blog files with -h option set now:

gsutil -h "Cache-Control:public, max-age=600" rsync -d -r . gs://<bucket_name>/

If you need to update/set Cache-Control (or any other header you wish) for files already uploaded to a bucket you can use the following command:

gsutil setmeta -r -h "Cache-Control:public, max-age=300" gs://<bucket_name>/

In order to prevent caching of html, xml and txt files I use the following command after I upload new version of a blog:

find . -name '*.html' -printf '%P\0' -o -name '*.txt' -printf '%P\0' -o -name '*.xml' -printf '%P\0' | xargs -0 -I {} gsutil setmeta -h "Cache-Control:private, no-store, no-cache, max-age=0" gs://<bucket_name>/{}

-o option allows to provide multiple extensions to find, -printf '%P\0' gets rid of leading ./s in find results and terminates each result with \0 character. xargs picks up results devided by \0s (-0 option) and calls gsutil setmeta for each of the files.