Trouble with static sites and Cloudfront Caching

I’m trying to build out a static site that consists of a few sub-apps, all of which can be deployed entirely as static assets.

Everything gets synced up to an S3 bucket with a structure like this:

/index.html
/foo
/index.html
/foo.[hash].js
/bar
/index.html
/bar.[hash].js

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?


Related discussions:

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.

Check out these 2 links for more information on Cache-Control:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist
https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en#cache-control

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).

It might be easier to control, but this means explicitly setting the cache-control attribute on all html files, every time I deploy a new version?

It will work.

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.

A temporary fix may be to use cyberduck and invalidate newly updated files.

How are you building the static site? If you are using Jekyll you can handle invalidation and meta attributes on s3 objects automatically.

Built from Webpack and synced with the AWS CLI.

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.

Its not too difficult if you create a script to do it