0

I have exactly the same problem as author of this question: Preventing iframe caching in browser - url within my iframe is cached. I request sort of an overkill anti-caching policy in my headers and Fiddler shows the youtube video (the page within the iframe) is not cached either, just the URL is the same every time I reload the page with the iframe.

What is different? The few answers for the linked question that might work suppose the iframe is built dynamically by JavaScript. But I made my iframe as a plain, static HTML, just with few parameters inserted dynamically by php. Making just this one feature of whole website (it is not big) through JavaScript would be possible as a workaround, but I would rather stick with php/html.

So: is it possible to prevent caching of URL within an iframe without JavaScript? If yes, how?

EDIT - my code:

  $videoUrl = 'xL0NjTDgwY4';
  $x = rand();
  echo '<iframe id="player2" type="text/html" ';
  echo 'src="http://www.youtube.com/embed/'.$videoUrl.'?enablejsapi=1&origin=http://e-history.cz&n="'.$x;
  echo '></iframe>';

Fiddler shows:

Host: www.youtube.com
URL: /embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com
Caching: no-cache; Expires: Tue, 27 Apr 1971 19:44:06 EST
Caching for the outer page on my web: no-store, no-cache, must-revalidate, post-check=0, pre-check=0; Expires: Thu, 19 Nov 1981 08:52:00 GMT

Ask for other fields if needed.

The URL shown by Fiddler is the URL I had there for the first time, when I copied an iframe template from YouTube Player API (the second code block in the link, where iframe in plain HTML is shown.

My caching related headers (in .htaccess):

Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"

And in meta:

  <meta http-equiv="pragma" content="no-cache" />
  <meta http-equiv="cache-control" content="no-cache" />

The expiration date shown in Fiddler suggests that I ordered even more anti-caching headers when I though I had a caching problem. It is theoretically possible that these duplicities cause the problem, though it seems unlikely.

One more information that might be relevant: I have another iframe on my website, and it works as it should, no caching except for what is usual for Google Maps. Code for this working iframe:

  if(isset($kml)) {
    $adr = 'https://maps.google.com?q='.$kml.'&amp;ll=';
  } else  {
    $adr = 'https://maps.google.com?ll=';
  }    
  $adr .= $lat.','.$lon.'&amp;z='.$zoom;  
  $adr .= '&amp;t=h&amp;output=embed';
  echo '<iframe class="mapa" src="'.$adr.'"></iframe>';
Community
  • 1
  • 1
Pavel V.
  • 2,653
  • 10
  • 43
  • 74
  • you could add a random get string to the end of the url on each load to trick the browser into thinking its a different page. – cmorrissey Jan 29 '14 at 21:09
  • @ChristopherMorrissey: I tried this, or even to insert a completely different address, even a different domain. Fiddler still shows the same address being requested. – Pavel V. Jan 29 '14 at 21:13
  • Maybe if you show your code, someone will see something that needs to be done a little differently. – TecBrat Jan 29 '14 at 21:19

2 Answers2

3

I'm puzzled, but happy - it works now. The iframe was stuck on Wednesday (when I asked) and yesterday morning. Then I commented it out for almost whole day. When I uncommented it today (31 Jan 2014), it was OK - I can switch between URLs freely now.

The problem is resolved for me. I'm not absolutely sure, but I think it was Firefox bug 356558. Since it hasn't been resolved for years, I guess there's no easy solution, and I found a workaround in waiting for the cache to fade away. Anyway, I'm not going to accept any answer now, because there can be a faster non-JavaScript workaround, that could help others with the same problem.

Pavel V.
  • 2,653
  • 10
  • 43
  • 74
  • Firefox is a tag on your posted question, but I did not see it stated that this problem was unique to Firefox and not to other browsers. Was that the case? Did your problem only occur with the Firefox browser and not with other browsers like Chrome or IE? – JohnH Mar 24 '17 at 13:49
1

Try adding an expires header set to -1. This will expire the page once it has been sent from the server, invalidating the browser cache. You should also set the correct Pragma cache headers.

shaunl
  • 126
  • 3