No more JS / CSS browser cache refresh pain
We often have the following problem :
- we make a release (or not) and put it to production
As we do not have great memory and as our customer’s Helpdesk was tired of doing remote cache flush, we wanted to do something (automatic) about it.
Plone adds cache keys as suffixes for CSS and JS files, as for example base-cachekey6247.css.
This cache key doesn’t change, even after an instance restart. This is why browsers can keep the “old” versions cached.
To avoid that (and without touching to Apache or whatever), we wrote a little script that exectutes at the end of the instance startup process.It forces the “cook” of resources which generates a new cache key so that browser cannot cache CSS and JS anymore !
This script can of course be improved, but it fits our needs.
[Please note that this code is written for Plone 3.]
First, we subscribe to IDatabaseOpenedWithRootEvent (too bad we couldn’t use IProcessStarting because we have no context to get Plone sites) :
<subscriber for="zope.app.appsetup.interfaces.IDatabaseOpenedWithRootEvent" handler=".resources.clear_cache" />
Then, our handler gets the work done :
And voilà, no more browser cache flush hassle for our customer and their Helpdesk
If you have any thoughts on this, or think that this should be packaged in a collective egg (useful for you ?), feel free to comment.