Hash Cache: Something Familiar From Web on Swift
Since developing this blog using the awesome static website generator Publish, and putting it behind CloudFront, I realized that some special treatment was going to be needed to ensure updated site assets and CSS did not end up perma-cached.
The Feature/Bug
The caching that comes along with CloudFront is great. It helps with latency, and allows for the quick delivery of content assets.
However, when updating content, if the name/URL stays the same, the content will not be reflected to clients. You can manually invalidate these entries, and I imagine this could be automated.
But why not let locations and clients keep using the cached version if it didn't change, and only break the cache for both new content, and it's usage?
Familiar To Web
A strategy used for "single page apps", or statically generated Web/JS bundles, is to revise filenames to be named after a hash of their contents.
You can see this explained very nicely in Parcel's documentation.
This ensures if a revised file is being served, it will have a name that is a fingerprint of it's content. This will ensure the correct content is shown.
Integration Using Swift
I wanted to enable this behavior for my static site generation using Publish.
So as of this post, the site is being published using a Swift library I created, called Gorget!
Gorget is a Swift library enabling content hashing of static asset files for websites
It was written in Swift using Swift Concurrency. It will benefit from optimizations as I go. I tried to strike a pragmatic balance for it's non-crypto hashing needs by using CryptoKit's older SHA1 algorithm. But I'd like to use xxHash in the future.
The integration with Publish is very lean and not super vend-able right now. (It's just a local "Publishing step")