The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Headers to cache JavaScript
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 Code:
Code:
<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> It doesn't cache. Do I have a problem with my headers or is there some feature of vBulletin that prevents caching? |
#2
|
|||
|
|||
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.
|
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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. |
#5
|
|||
|
|||
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 Code:
<!--script type="text/javascript" src="{vb:raw vboptions.bburl}/clientscript/test-secret.js"></script--> <script> alert('s = ' + secret); </script> |
#6
|
|||
|
|||
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.
|
#7
|
|||
|
|||
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.
|
#8
|
|||
|
|||
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.
|
#9
|
|||
|
|||
I found something that almost works:
Code:
<script src="{vb:raw vboptions.bburl}/cache.php"></script> <script> alert('x = ' + testVar) </script> 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? |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|