Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent external files in src of iframe from being cached (with CloudFlare)

I am trying to make a playground like plunker. I just noticed a very odd behavior on production (with active mode in Cloudflare), whereas it works well in localhost.

By iframe, the playground previews index_internal.html which may contain links to other files (eg, .html, .js, .css). iframe is able to interpret external links such as <script src="script.js"></script>.

So each time a user modifies their file (eg, script.js) on the editor, my program saves the new file into a temporary folder on the server, then refresh the iframe by iframe.src = iframe.src, it works well on localhost.

However, I realized that, in production, the browser always keeps loading the initial script.js, even though users modify it in the editor and a new version is written in the folder in the server. For example, what I see in Dev Tools ==> Network is always the initial version of script.js, whereas I can check the new version of script.js saved in the server by less on the left hand.

Does anyone know why it is like this? And how to fix it?

enter image description here

Edit 1:

I tried the following, which did not work with script.js:

var iframe = document.getElementById('myiframe');
iframe.contentWindow.location.reload(true);
iframe.contentDocument.location.reload(true);
iframe.contentWindow.location.href = iframe.contentWindow.location.href
iframe.contentWindow.src = iframe.contentWindow.src
iframe.contentWindow.location.replace(iframe.contentWindow.location.href)

I tried to add a version, it worked with index_internal.html, but did not reload script.js:

var newSrc = iframe.src.split('?')[0] 
iframe.src = newSrc + "?uid=" + Math.floor((Math.random() * 100000) + 1);

If I turn Cloudflare to development mode, script.js is reloaded, but I do want to keep Cloudflare in active mode.

like image 613
SoftTimur Avatar asked Oct 29 '22 07:10

SoftTimur


1 Answers

I found it.

We can create a custom rule for caching in CloudFlare:

https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-#cache

For example, I could set Bypass as Cache Level for the folder www.mysite.com/tmp/*.

like image 172
SoftTimur Avatar answered Nov 15 '22 08:11

SoftTimur