Will Perone

Disabling SWF Caching in your Browser

During your Flash development you have probably noticed that when you test your SWF in the Browser that the browser never seems to update the Flash app to the one you just uploaded to your server. This is because the browser actually caches the SWF file and doesn't know that it has changed on the server. There is a way however to provide a PHP wrapper around the SWF to change this behavior. All you need to do is write a game.php that does the following:
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('game.swf');
This php will essentially act as a mediator between the browser request and the server. The browser still thinks this file is a SWF type request but the server tells the browser that additionally it should not cache the file. All you need to do now is just call the PHP file instead of the SWF file from your HTML page that displays the Flash app.
<object width="640" height="480" align="middle">
<param name="movie" value="game.php" />
<param name="bgcolor" value="#000000" />
<embed src="game.php" width="640" height="480" scale="noscale" bgcolor="#000000"
       type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
Vuala, you now have a Flash app that reloads itself every request. You alternately may set the timeout/expire time to something like a week or whatever if you are updating your production Flash app every so often to force your users to update to the newest version on a somewhat consistent basis.

Now the same thing tends to happen inside of Flash when you do a URLRequest. You would think that something like the following would work to disable caching:
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(whatever url);
request.requestHeaders.push(new URLRequestHeader("pragma", "no-cache"));
request.requestHeaders.push(new URLRequestHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT, -1"));
request.requestHeaders.push(new URLRequestHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
WRONG! For some reason, Flash doesn't respect any caching rules; the only way to get around this is to trick Flash into thinking your url is a unique url every time.
var request:URLRequest = new URLRequest("whateverurl?nocache=" + new Date().getTime());
This method has a drawback too though; if you are using URLVariables, there will be a conflict. The solution is to just make a URLVariable with our nocache trick:
var request:URLRequest = new URLRequest(whateverurl);
var variables:URLVariables = new URLVariables();  
variables.nocache = new Date().getTime(); 
// set up other URLVariables here
request.data = variables;
There you go, caching is now disabled!

s11im 26 80
Is it possible to set no-caching with only actionscript. I do not have access to PHP at the base where i work.
Will 0 0
You can't disable the browser caching for content from actionscript; all you can do in there is turn off the caching for network requests from inside the swf. If your webpage is one big swf that just fetches content, then you're pretty much in control of all the caching policies.
s11im 10 8
Thanks for the quick response will. Here is some more background information on what i am doing. I have made a calendar that interacts with Adobe Connect Via the XML api. The action script makes various calls for reports and meeting information. It works fine in a http envrionment on IE however when migrated to a HTTPS envrionment my xml calls fail to produce successful results. would turning off the caching for network request from inside the swf fix this problem. and would the above code work to do this PHP Free.
Will 21 3
That doesn't sound like a caching problem. It might be an issue with your crossdomain policy file. It is usually problematic to mix http and https with Flash. Is your swf sitting on a http server and making a https request?
Kit 8 7
Thanks x 10000000000000000000000000000000000000000000000000000000000000000000000000000000
Brad 8 96
Thanks a million, this singlehandedly solved my caching problems - which were screwing up my dynamic positionings pretty bad. Thanks a million!
xtrykx 0 0
Isn't this a better solution?
<meta http-equiv="pragma" content="no-cache" />
xtrykx 0 0
"<meta http-equiv="pragma" content="no-cache" />"
xtrykx 0 0
&#60;meta http-equiv=&#34;pragma&#34; content=&#34;no-cache&#34; /&#62;
d 0 0
nice one. was about to go mad trying to figure this out!
mitcHosch 2 2
Thank you so much for the solution provided by this code snippet : "var request:URLRequest = new URLRequest("whateverurl?nocache=" + new Date().getTime());" You can't imagine how much you have helped me. I am in your debt. Do you a PayPal account for a donation?
mitcHosch 0 0
Never mind. I see you do. Thanks again! :-D
sebs 11 11
this .htaccess thing works well for me. It sets the caching to 30 seconds:

# 3 Seconds
<FilesMatch "\.(swf|xml)$">
Header set Cache-Control "max-age=30"

5 min = 600..
sebs 0 0
P.S. You need to put the above code in the .htaccess file that is in the same directory as your swf/xml file. With this you can separate content that needs to be cached and content that doesn't need caching.
Artyom 0 0
Thank you very-very much!!!!!!!!!!
Leonardo 0 0
Thank you very much!!!
You don't know how I'm very grateful now =)
Batigoal 0 0
you're a legend. Thanks a bunch
Your Name 0 0
Thank you!!!!
Kenan 2010/10/11 Contact Me9 9
Hey, thanks.

The first PHP no caching thing worked perfectly (I have an SWF file with size of 500 KB which will never be cached).

But from this first SWF file, I am loading by using Loader class another smaller SWF which I also want it not to be cached. but it is being cached.

this is the code:
var request:URLRequest = new URLRequest("content/loansAr.swf");
var variables:URLVariables = new URLVariables();
variables.nocache = new Date().getTime();
// set up other URLVariables here
request.data = variables;

var loader:Loader = new Loader();

Can you help me with this?
Will 2010/10/11 Contact Me1 2
I haven't tried checking caching for swfs loading inside swfs but here's some stuff to try:
- try adding the random parameter to the actual url string
- try explicitly setting request.method=URLRequestMethod.GET;
If all of those fail you may want to actually version the filename if possible, like have loansAr-v1.swf, loans-Ar-v2.swf etc.
King 2010/10/310 0
See this:

SparK 2010/12/02 Contact Me0 0
try adding the date hack in your urlrequest directly, with "content/loansAr.swf?nochache="+new Date().getTime());
SparK 2010/12/02 Contact Me0 2
oh btw, why not just use getTimer()? it's the miliseconds passed since flash was open

you can use "?c="+getTimer(); or something for short
qilania 2011/02/0811 0
If you have the option to mess with server config, you can setup this for all files with no extra coding or workarounds. Similar to sebs's solution, but server-wide instead of per-directory options. This works for Apache:

<FilesMatch "\.(swf)$">
ExpiresDefault A0
Header append Cache-Control "must-revalidate, proxy-revalidate"

Teorically, the browser will see if its cached file is older than the one served by Apache (taking Etags also into consideration). If it is, it will re-download it. Also valid for URLRequests, on my experience. Requires the "headers" and "expires" modules enabled.
Casaubon 2011/03/16 Contact Me0 0
Will 2011/03/16 Contact Me0 0
Casaubon, are you able to trace out the values of loaded and total individually to see which one is invalid?
Casaubon 2011/03/17 Contact Me0 0
Will 2011/03/17 Contact Me0 0
The total size is 0 because the content-length header isn't set, try adding this to the php script before the echo:
header("Content-Length: ".filesize("game.swf"));
Casaubon 2011/03/18 Contact Me0 0
works like a charm now! (first I had double quotes instead of single ones in this php line, which resulted in a white screen. it took me a while to realize what was causing this)
thank you very much Will, the next weeseltown character I add to the site is gonna be a Will, if you are ok with that - to show my gratitude :-)
Will 2011/03/18 Contact Me0 0
Hah sure, thanks! I wonder what a pixelized Will would look like...
fernando 2011/06/280 0
you're a genius thank's a million!
Mark 2011/06/30 Contact Me0 0
Thanks Will...so easy to implement...even for a novice like me
Kerryn 2011/07/08 Contact Me0 0
Gee willireks, thatÂ’s such a great post!
Page 2011/09/07 Contact Me0 0
I have my page redirect based upon the day of the week and the secretary places a daily bulletin named for that day of the week on the server in the format of swf. It is not kicking out the old swf file and replacing it with the new one. Is there a simple fix for forcing the page to load a fresh one? I have used the head metatag for no cache without success. Please help
Will 2011/09/100 0
The flash cache (even externally from the browser perspective) works a bit differently than the normal browser cache; it is stored with the default browser cache (not necessarily the browser the user is using); this would be IE on windows and Safari on mac. Best to just append a query string to the flash file that is the current date in your case. Ex: bulletin.swf?v=2011-9-10 then when you push out a new version just change that tag (or have it automatically update)
Arvin 2011/09/151 0
Hey Thanks very helpful post ......
You have saved my day.... :)
sunwukong 2011/09/15 Contact Me0 0
Thank you, I slept on this problem last night, knowing that flash was caching my php access and not reflecting the changes I was making. Woke up this morning, googled you, and BAM I have satisfaction. :)
2011/09/160 0
I found a site that gave code to add to the end of the URL that adds a simple random number that tricks the browser into thinking that it is something new each time. Worked like a charm.

For the java it was


<!-- Begin
function initArray() {
this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++)
this[i+1] = initArray.arguments[i];
var DOWArray = new
var today = new Date();
var day = DOWArray[today.getDay()+1];
window.location = day + ".swf" + "?" + today;
// End -->

the + "?" + today part

it worked best for the JAVASCRIPT than it did for the HTML

within a page an embedded flash file I used this code

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="100%" height="100%" title="Monday Bulletin">
<param name="movie" value="sunday.swf?"+Math.random()
<param name="quality" value="high" />
<embed src="sunday.swf?"+Math.random()
quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="309" height="400"></embed>
Page 2011/09/160 0
The above was from me
SparK 2011/09/16 Contact Me0 0
oh man... nobody read my comment about adding the seconds to the url =(
really, it's up there!
Page 2011/09/220 0
Thanks Spark, that timer helped a lot
Jiinus 2011/09/262 0
SparK: I wouldn't recommend using getTimer() here. Although it probably works most of the time, it's not bulletproof. If your app happens to have run exactly the same amount of time at the time of loading as before, you'll get an expired version. Same problem with Math.random(). I would recommend using a value based the current date/time which is kind of guaranteed to never be the same twice.
Page 2011/11/070 0
an example of the current date/time would be? date()time()
Will 2011/11/072 0
The date/time method is what is demonstrated above: new Date().getTime()
Tompa 2012/05/01 Contact Me172 10
Thanks alot!
I've been having problem with this part on many of my projects!
Anuntul dvs pe intenet 2012/09/15 Contact Me0 0
What's up colleagues, how is the whole thing, and what you wish for to say on the topic of this article, in my view its actually amazing in support of me.
LAHOMA 2013/11/08 Contact Me0 0
First of all I would like to say awesome blog! I had a quick question in which I'd like to ask if you don't mind. I was curious to know how you center yourself and clear your mind prior to writing. I've had a hard time clearing my thoughts in getting my ideas out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually lost simply just trying to figure out how to begin. Any recommendations or hints? Thanks!
LAHOMA http://www.christianlouboutinshoeoutletonline.com
FoxLift 2014/01/073 5
Hey buddy, just wanted to say that this helped me out in the problem I had. Thanks!
Smile 2014/02/231 0
Sir.with all gratitude and Respect..
thank you so much for this tutorial...
it really helped us a lot again thank you
fds 2014/04/23 Contact Me0 0
2015/10/030 0
Really a good trick! Thanks :)

<- for private contact