A good way to disable swf browser cache

I found a very cool way to disable the browser cache for my .swf files. When I have searched for a solution for this problem, I found that you can add a dummy parameter, like timestamp or date, to your swf file and the browser will re-download the file. This solution is wide spread on every site that offer a solution for this issue, but I don’t like it because the browser still  cache your swf file, creating multiple cache files for a single .swf file. So, after reading a lot of blogs I found the following solution:

Create a php file called swfloader.php (Yes, a PHP file) containing:

header('Content-type: application/x-shockwave-flash');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT, -1');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
echo file_get_contents('Test.swf'); // <- add here your .swf file

You can find on wiki a list of all http headers.

Now, in the file where you embed the .swf file and replace the .swf file with the php file.

Note. I’m using the swfObject.js to embed my .swf files, but the same replace can be made if you are using the object tag.

swfobject.embedSWF("swfloader.php", "myDiv", "100%", "100%", "10.0.0",{},flashvars,{});

If you are loading external .swf file in your main swf I will be better te specify that dummy parameter because Flash player doesn’t respect the caching rules.

var moduleLoader :ModuleLoader = new ModuleLoader();
// You can use the same randomFunction if you are using URLLoader class.

Now, your browser should not cache any of your swf files. 😉

This entry was posted in Flex and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *