Then to enable HTTPS I’m running a Cloudfront distribution above the S3 bucket.
I don’t want Cloudfront to try and cache the index.html files at all, because they aren’t fingerprinted. At the moment, when I push a new change up to the site, users are still being served cached HTML, which references the old assets.
I added a cache behaviour to the *.html path with a TTL of 0, but this seemed to have no effect at all.
Looking at the popular objects report, it seems that the sub-apps are requested as /foo and /bar rather than /foo/index.html and /bar/index.html which explains why the cache behaviour isn’t being applied.
Short of doing manual cache invalidations after each deploy, or creating specific cache behaviours for each of the sub-app routes, what’s the best way to make sure Cloudfront is always serving the correct index.html files?
Don’t use behaviors for this, set the Cache-Control attribute on the S3 objects instead. Much easier to control that way. “Cache-Control: no-cache” will make CloudFront revalidate (check if the file changed in S3) on every client request.
I actually think setting cache behaviours is a better way to go about this - less overhead than having to set the cache-control on every file you upload, and possibly forgetting some.
I haven’t tested this, but I’m pretty sure if you set a behaviour just on index.html, it will set it on all files names index.html. Otherwise you can use wildcards, e.g. */index.html
As I understand the wildcard works, but the problem is that the cache invalidation isn’t applied, because the behaviour path is *.html, whereas the requested object path is /foo (which serves /foo/index.html).
Create a custom behaviour for either index.html or */index.html (you’ll have to test which one works). The custom behaviour should set Min, Max and Default TTLs to 0.
I know it will work because I do this with my own company’s UI. Granted, we don’t do it for index.html in any subdirectories, but the wildcard should take care of that if specifying just the plain filename doesn’t.
You may be able to automate the setting of metadata at some point in that process. I use https://github.com/laurilehmijoki/s3_website for Jekyll and it seems to be able to do some of these things.