Why does it seem like Google App Engine isn’t setting appropriate cache-friendly headers (like far-future expiration dates) on my CSS stylesheets and JavaScript files? When does GAE gzip those files? My app.yaml
marks the respective directories as static_dir
s, so the lack of far-future expiration dates is kind of surprising to me.
This is a community wiki to showcase the best practices regarding static file caching and gzipping on GAE!
How does GAE handle caching?
It seems GAE sets near-future cache expiration times, but does use the
etag
header. This is used so browsers can ask, “Has this file changed since when it had aetag
ofX68f0o
?” and hear “Nope –304 Not Modified
” back in response.As opposed to far-future expiration dates, this has the following trade-offs:
Opting for far-future cache expiration instead of (just)
etag
To use far-future expiration dates takes two steps and a bit of understanding.
You have to manually update your app to request new versions of resources, by e.g. naming files like
mysitesstyles.2011-02-11T0411.css
instead ofmysitestyles.css
. There are tools to help automate this, but I’m not aware of any that directly relate to GAE.Configure GAE to set the expiration times you want by using
default_expiration
and/orexpiration
inapp.yaml
. GAE docs on static filesA third option: Application manifests
Cache manifests are an HTML5 feature that overrides cache headers. MDN article, DiveIntoHTML5, W3C. This affects more than just your script and style files' caching, however. Use with care!
When does GAE
gzip
?According to Google’s FAQ,
Some real-world observations do show this to generally be true. Assuming a gzip-capable browser:
content-type
headers liketext/html; charset=utf-8
)static_dir
s (defined inapp.yaml
).