Requirements:
- Assets are accessed unfingerprinted (e.g.
/assets/application.js
) - Rack::Cache is enabled
- Assets are gzipped
- Assets are precompiled
- Assets are sent to the browser when updated in source control / the filesystem
This is not as easy as it sounds. By default, when rack cache is enabled, files requested from the filesystem via Rack::Static
are cached, and therefore when the file changes, those changes are not reflected in the cache, and stale assets are served up.
My solution is to provide a special store to Rack::Cache, which checks if the cached content being check matches a pattern, and sets a key prefix. I then have an incrementing version value that is checked into source control, which will change the key path, and allow for fresh content to be served up.
This store allows any proc to be passed, and the proc will be called, with the cache key, and can return a prefix to be applied.
This extends PrefixStore, and supplies a proc which checks if the requested key matches /assets/