Monday July 14, 2003
Prevent Caching of JavaScript and CSS files We've been having an issue at work for awhile now where our .css and .js files are cached by a proxy server. When we update the app, we get a few users (behind the proxy server) that get served up an old style/script file, and the app looks like it's broken. So I added a super-simple cache-killer to our .js and .css files today. In my taglibs.jsp (included in every JSP), I added:
<%-- Create a variable that is the current time (in milliseconds) to kill
caching on the proxy server --%>
<jsp:useBean id="now" class="java.util.Date" />
<c:set var="cacheKiller">
<fmt:formatDate value="${now}" pattern="yyyyMMdd"/>
</c:set>
My date pattern only goes to the day because we don't update the site more than once in the same day. This way, the users will still get the stylesheet/script caching benefit of the browser, but now we control when the file is reloaded, rather than the proxy server. To make sure these files are re-fetched every request, you could use pattern="yyyyMMddHHmmssS" to get all the way down to the millisecond. After adding this, I adjusted my baseLayout.jsp (Tiles template) to add my cacheKiller as a parameter to the src attributes of scripts and stylesheets.
<%-- Get Javascript List --%>
<tiles:useAttribute name="scripts" ignore="true"/>
<c:forEach var="js" items="${scripts}">
<script type="text/javascript"
src="<html-el:rewrite page="${js}"/>?<c:out
value="${cacheKiller}"/>"></script>
</c:forEach>
Works like a charm! Posted in Java at Jul 14 2003, 10:16:48 AM MDT 6 Comments
Search This Site
Recent Entries
- Happy Birthday Mom!
- 2008 - A Year in Review
- Portland Tech Meetup Tomorrow Night
- My "almost slept in a snow cave" Adventure with Clint Foster
- My Christmas Travel Adventure
- AppFuse Light converted to Maven modules, upgraded to Tapestry 5 and Stripes 1.5
- Dojo/Comet support in Java Web Frameworks
- Abbie is a Blue Skier!
- How I recovered data from my failed Linux box
- Costa Rica was Awesome!
Posted by matt on July 14, 2003 at 01:23 PM MDT #
Posted by Matt Raible on July 14, 2003 at 02:03 PM MDT #
Posted by Dan Martin on July 14, 2003 at 04:40 PM MDT #
Posted by Matt Raible on July 14, 2003 at 04:59 PM MDT #
Posted by Scott Farquhar on July 14, 2003 at 09:08 PM MDT #
Posted by Sam Hough on July 20, 2003 at 02:08 PM MDT #