Easy Static Asset Expiration in J2EE

Just ran into a defect that came up because QA’s web browser had cached an old version of a JS file. I was hoping to find an easy way to configure caching for static assets in my webapp. Turns out that there’s a J2EE cache-filter that does it quite easily. Just add it to your pom or lib directory, then add a few filters and filter-mappings to your web.xml, and you’re good to go!

I configured 1 hr caching for JS and CSS, so they’ll be refreshed by the time I do a QA build. You could also set up your build script to make your cache settings environment-specific, so prod caches longer than QA. You can also use this to set longer caches on things like images (I set ’em to cache for 1 day), to increase performance.

Advertisements

One Response to Easy Static Asset Expiration in J2EE

  1. Eric Rich says:

    Using cache-filter is a good option. Here’s something else to consider too. We append the version of our software to the end of the URL when requesting JS and CSS resources from HTML documents. The version is appended to the URL using a Velocity macro. This forces the web browser to download the latest version of the resources for each build of the software.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: