PDA

View Full Version : Headers to cache JavaScript


nerbert
03-04-2013, 01:47 PM
I have a member I have to suspend from time to time and during the suspension he tries registering several times a day. My plan is to cache some JavaScript on his computer to identify him and abort his registration attempts.

My other admin suggested testing for a JS variable and if it has no value use AJAX to call a PHP file whose output is treated as a JS file that will be cached. Using the headers he suggested, I created a file, cache.php, to test caching:


<?php
$farfuture = mktime(0,0,0,1,1,2030);
header("Cache-control: public",true);
header("Pragma:",true);
header("Last-modified:" . date("D, d M Y H:i:s",$farfuture) . "GMT", true);
header("Expires:" . date("D, d M Y H:i:s",$farfuture) . "GMT" , true);
header('Content-Type: text/javascript; charset=utf-8');
?>
var testVar = "test";


and I request it with AJAX in the headinclude template with this:


<script>
if(typeof testVar == 'undefined') {
var getCache = new vB_AJAX_Handler(true);
getCache.onreadystatechange(function() {
alert(getCache.handler.responseText);
});
getCache.send('cache.php');
} else {
alert('t = ' + testVar);
}

</script>


So if it fails to cache I should get "var testVar = "test";" and if it caches I should get "t = test".

It doesn't cache. Do I have a problem with my headers or is there some feature of vBulletin that prevents caching?

kh99
03-04-2013, 02:46 PM
I'm not a js or caching expert or anything, so maybe I just don't understand how it's supposed to work. But I don't see how that is supposed to detect whether or not a file is cached by the browser. If it's cached it just means that loading it won't require a request to the server, but it doesn't mean that it will already be loaded for you. So it seems to me that testVar would always be undefined.

nerbert
03-04-2013, 03:13 PM
He says that JS files are generally cached on your browser so they don't have to be loaded from the server every time. So if a file is missing it should be available in your browser cache.

This is supposed to cache the simple one line file giving a value to testVar.

Maybe I should try manually adding and then removing a test file using <script src="my-test-file.js"></script> and see what happens

kh99
03-04-2013, 03:15 PM
I tried to add this to the above post but for some reason I'm having a problem. Anyway:

It is an interesting idea. It seems like what you'd want to do is something like, have cache.php return code that sets a variable to a different value depending on whether or not that particular user is loading it, then just load it with <script src="cache.php"> and check the value of the variable. Also maybe set it to not cache if a guest loads it, that way it won't get cached before the user logs in.

nerbert
03-04-2013, 03:38 PM
I'm having trouble using Quick Reply here. There seems to be some AJAX problem here.

If I can get caching to work at all I'll put in various conditionals and store the user's name and id. But for now caching doesn't seem to work. I tried


<!--script type="text/javascript" src="{vb:raw vboptions.bburl}/clientscript/test-secret.js"></script-->
<script>
alert('s = ' + secret);
</script>


and alternating whether the script tags linking to the file are commented or not and caching still isn't working. (The file just gives a value to the secret variable)

kh99
03-04-2013, 03:45 PM
and alternating whether the script tags linking to the file are commented or not and caching still isn't working.

Well, that's what I meant. If you comment out the script tags that load the file, it doesn't matter if it's cached or not, the code won't be loaded.

nerbert
03-04-2013, 04:01 PM
OK I see what you mean. Instead of commenting the script tags I changed the file name and now it gets the value of secret from cache. I'll try putting in the tags you suggested for cach.php and see what happens.

kh99
03-04-2013, 04:50 PM
Again I'm not an expert on the subject, but I think, depending on browser settings, the browser can send a request to check if the file has changed. So to make it work you might have to detect that request and return a "not changed" response. I think that's what's going on at the beginning of image.php, so you might want to look at that.

nerbert
03-04-2013, 09:47 PM
I found something that almost works:


<script src="{vb:raw vboptions.bburl}/cache.php"></script>
<script>
alert('x = ' + testVar)
</script>


No AJAX necessary.

Since cache.php is a php file I can require global.php and set testVar to the userid. After logging out that value is preserved for a few refreshes and then for some reason it loads the file from the server and returns a userid = 0 as it should for a guest.

Does anyone know how the software would direct the browser to reload a cached file after several page refreshes? Or would that be a browser setting?