Cache control tips for Flash

Working on my main site I realize that Google Chrome NEVER EMPTY IT’S CACHE, never, and, I don’t know if it’s a html cache control problem or what, but, I make changes to my site and every time I refresh the page (still on chorme, actually Firefox some times does the same) the page still the same, I tried meta and php headers for cache control but still the same, more specific, loaded xml, images, mp3, etc.

I decided to do some research surfing on the web a lot, testing a lot more and here is a series of tips to deal with really dynamic content for Flash. I use all of this guidelines together. If there is other ways you know to avoid caching issues you are free to leave comments and links, except for those trying to convince me to buy some cheap meds, enlarge stuff or download a lot of aerosmith songs for free…

HTML meta tags and Server side Headers

Add this meta tags inside the head tag on your website, after the title tag, just to know where you place it

<meta http-equiv="Expires" content ="0" />
<meta http-equiv="Pragma" content ="no-cache" />
<meta http-equiv="Cache-Control" content ="no-cache" />

Ok, this is the first part. You can actually send some http headers using the proper fuction depending on your prefered server side langage.

PHP

header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Note: gmdate php function gets the current datetime mark, I put a series of more info links at the end of this post.

ASP

<% @Language="VBScript" %>
<% Response.CacheControl = "no-cache" %>

ASP.NET (VB)

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetNoStore()

Here is other more agressive ASP.NET code (C#)

Response.ClearHeaders();
Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1
Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1

I’m not writing every known language cause sintax are similar and each web based language has a method to send headers.

Loading the SWF

This method requires in this case SWFObject 2+ to load the swf movie, the concept is, you can request any file followed by a unique request, for example, like doing a GET to file.php?id=, adding the GET variable with a random number results in a diferent request even if you are requesting the same file, the server will response like if it’s a new file, or the file content changed.

Here is the code

 //your cache control var
 //remember to use Math.floor to remove floating points
var cachecontrol = Math.floor(Math.random()*99999);
var flashvars = {};
var params = {};
var attributes = {id:"movie"};
swfobject.embedSWF("movie.swf?"+cachecontrol, "flashcontent", "100%", "100%", "10.1.0", "expressInstall.swf", flashvars, params, attributes);

Loading assets with Action script

I will assume that you know how to load files with Actionscript 3. Like in the last tip, we are requesting a file with a unique id (kind) on the URLRequest parameters

//loader stuff
var cachecontrol:String = Math.floor(Math.random()*9999).toString();
loader.load(new URLRequest("asset.<ext>?uid="+cachecontrol));

.ext refers to a valid file extension for the proper class like Loader, URLLoader or Sound, I’m 90% sure that also can work with multiple loader solutions like BulkLoader.

If you are on AIR development, you can set some code to check the local cache before making a new request, or simple disable the cache but remember this ir AIR only.

URLRequestDefaults.useCache = false; //air only ¬¬

I hope this basic tips can help you guys.

For more info on all this stuff you can check the folowing links

Actionscript reference on loading content
PHP Header function
ASP.NET AddHeader method
Further reading about cache
HTTP Request


Share