The purpose of this page is to describe one strategy for caching of static resources in a JSP based web application.
The term "assets" is used to denote static files javascript, CSS, images, etc.
Credit go to Trygve Laugstøl and Erlend Hamnaberg
for describing the concepts and suggesting implementations.
What
- External assets should be cached forever.
- Internal images should be cached forever.
- Internal JavaScript and CSS files should be reloaded for every new release.
- Minify JavaScript and CSS
Abstract implementation steps
- Ensure internal and external assets are put in separate folders.
- Ensure all external assets are versioned
- version in folder OR
- version in filename
- Ensure internal images are versioned if changed.
- Use auto-versioning
for internal assets.
- Set Cache-Control headers
- Remove ETags and Last-Modified headers (to increase chance of browsers have the same caching behaviour)
- Minify JavaScript and CSS build time
4. and 5. - Auto-version based on Tucket UrlRewriteFilter
pom.xml
Intermediate property file
ServletFilter to set session attribute
http://www.tuckey.org/urlrewrite/
In WEB-INF/urlrewrite.xml:
web.xml
Script tag in JSP file
Note the url encoding using c:url.
Resources
http://derek.io/blog/2009/auto-versioning-javascript-and-css-files/
Good guide to web caching: http://www.mnot.net/cache_docs/
http://stackoverflow.com/questions/2630885/auto-versioning-of-static-content-with-jboss
Labels:
None