12
Apr

Avoid user’s cache when deploying new versions of Flex app

As to continue my previous post, these days I’ve been trying to provide a solution that removes the ambiguity for the released swf files into production so that I know what version is deployed and also to make sure that the users are forced to get the last release, no matter of their browser’s cache.

Problem
When deploying new versions of the application, some users having cache in their browser don’t see the updated version of the swf, thus loosing the updates.

Solution
Automatically append the version number at the end of the url pointing to the swf file, ending with something like : Main.swf?173-1.0-SNAPSHOT. The advantage is that you benefit of the cache in-between new releases; this means that clients can view you flash application faster, and only for new releases they have to wait a little bit more to load.

Having the flex application packaged inside of a WAR file I’ve been looking for places to store this information inside of it, and read the build-number at runtime. I discovered that the MANIFEST.MF file, which is found inside the META-INF folder, is the best candidate.
In order to write the buildNumber into that file, I used maven-war-plugin:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<version>2.1-beta-1</version>
	   <executions>
		   <execution>
		        <phase>package</phase>
		        <goals>
		          	<goal>war</goal>
		        </goals>
		        <configuration>
		           <archive>
	          		<manifest>
	          		   <addDefaultSpecificationEntries>
	   	   	   	      true
	   	   	   	   </addDefaultSpecificationEntries>
	          		   <addDefaultSpecificationEntries>
	   	   	   	      true
	   	   	   	   </addDefaultSpecificationEntries>
	          		</manifest>
		          	<manifestEntries>
		            	   <Implementation-Build>
	   	   	   	      ${BUILD_NUMBER}
	   	   	   	   </Implementation-Build>
		          	</manifestEntries>
		           </archive>  
		        </configuration>
		</execution>
	</executions>
 </plugin>

${BUILD_NUMBER} is only available when executing the maven build from Hudson, as explained in my previous post. Maven also has a plugin that generates a build number automatically, but I didn’t need it because, in my case, no code goes into production without passing the Continuous Integration build and the tests, so I will have a build number for every promoted build.

Next, I had to read the information at runtime. For that I altered a bit the index.jsp file I am using to show the flex application:

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
 
<% 
java.util.jar.Manifest manifest = new java.util.jar.Manifest();
manifest.read(pageContext.getServletContext().
                                   getResourceAsStream("/META-INF/MANIFEST.MF"));
java.util.jar.Attributes attributes = manifest.getMainAttributes();
String fullVersion = attributes.getValue("Implementation-Build") + 
                 "-" + attributes.getValue("Specification-Version");
%>
 
<c:set var="swf" value="bin/MediaCenter"/>
<c:set var="version" value="<%=fullVersion%>" />
 
<%@ include file="/flex.jsp" %>

The flex.jsp file is taken from the FNA project hosted on Google Code. I’ve only altered a bit just to add the version number at the end of the request:

swfobject.embedSWF(
            "${ctx}/${swf}.swf?${version}", 
            "${application}_div", 
            "${width}", "${height}", 
            "${version_major}", xiSwfUrlStr, 
            flashvars, params, attributes);

That’s pretty much it. The result is:

Main.swf?173-1.0-SNAPSHOT

and it’s set automatically, for each build. This ensures that users, testers, everyone else see the right version of the application.

Note that I’m also using the Specification-Version, which is the same with maven version. The reason I’m doing this is because locally, on the dev machine, the ${BUILD_NUMBER} is null, and I simply didn’t want to see Main.swf?null in the url :). Specification-Version came for free with the Maven-war-plugin, so I decided to make use of it. The MANIFEST.MF file contains more information; you can have a look into that file, after deploying it.

1 Comment

Sorry, the comment form is closed at this time.

Comments RSS Feed