Fixed JS File Timestamp

xf_phantom

Well-known member
Wouldn't it be better to conciser something else as
PHP:
self::$jsVersion = substr(md5(self::$versionId . $config->jsVersion), 0, 8);
?
E.g. save the time of the last addon installation / xenforo update?


The problem is, that the $jsVersion isn't updating after addon installations.
Because of this, addon with own js files are buggy, because they return the cached version, instead of the new one, without hard browser cache refresh

It's no problem to change it via the config, BUT i wouldn't think that's a good way for avarage joe users, to say that to change their config file after my addon installation:D
 
Heh, I PMed the devs about this not too long ago coincidentally, but they did not reply. :p

I don't think that would be a good place to set the version though since I think that would globally change all query strings when an addon script changes. For example, if I make a tiny change in one of my JS scripts I have no need for that to recache all those chunky XF scripts as well. In my own addons what I do is just change the query strings for all addon scripts only if they change, and only for individual scripts as well. So if my addon has 10 JS files and I just update one of them, only the query string of that one file changes and not all 10, and definitely not all XF scripts. Considering that I update my addons A LOT I don't think it's worth it to recache all scripts when any of them updates. Unless they do something similar I think it's better kept the current way.
 
if I make a tiny change in one of my JS scripts I have no need for that to recache all those chunky XF scripts as well
I don't disagree, but i prefer to recache everything, instead of breaking the page because the users got the old js files


In my own addons what I do is just change the query strings for all addon scripts only if they change, and only for individual scripts as well.
You mean you're using something like

Code:
<xen:require js="js/addon/script1.js?a=1" />

That's indeed a nice workaround, thx for this

BUT it requires a reload of the addon files if xf was upgraded, because the default xenforo v variable is also added:D


What about changing
PHP:
protected function _processJsUrls(array $jsFiles)
{
// add version number to each required file
foreach ($jsFiles AS &$file)
{
$file = $file . (strpos($file, '?') ? '&' : '?') . '_v=' . XenForo_Application::$jsVersion;
}

and checking for _v=

if it's available, don't add the default jsVersion variable

e.g
Code:
<xen:require js="js/addon/script1.js?_v=1" />
wouldn't be changed

but this one
Code:
<xen:require js="js/addon/script1.js" />
would be
 
You mean you're using something like

Code:
<xen:require js="js/addon/script1.js?a=1" />

That's indeed a nice workaround, thx for this

BUT it requires a reload of the addon files if xf was upgraded, because the default xenforo v variable is also added:D
That isn't what I do, but that would be a quick way to do it, although it suffers from the issue like you stated, still better than recaching all XF files as well though.

The main issue I had with that method is because I'm lazy. :p I don't want to have to manually change any version strings whenever I update my files. There might be a better way to do it, but I just manipulate the scripts in template_post_render. I look for any non core js files and change the query string with a hashed version of filemtime instead. That way the query string changes whenever any addon js file is updated without any manual changes and it'll ignore XF core js files. I also had to hook into front_controller_post_view to do the same for scripts loaded through an overlay. Not sure how badly repeated calls to filemtime would affect a large board though...

If they change the current behaviour, I think they could do something better so I can junk my code. :)
 
I did a couple things here to make this easier. There's now an option you can update to force the standard JS to be recached:

Code:
XenForo_Model::create('XenForo_Model_Option')->updateOption('jsLastUpdate', XenForo_Application::$time);

This isn't done automatically by add-on updates as many situations may not need it.

Second, if your JS URL has _v= in it, we don't include our standard cache buster, but you'll still be responsible for updating that as necessary.
 
I did a couple things here to make this easier. There's now an option you can update to force the standard JS to be recached:

Code:
XenForo_Model::create('XenForo_Model_Option')->updateOption('jsLastUpdate', XenForo_Application::$time);
Does it also recache all the XF core js files as well? Or only addon js files? It sounds like this is for all XF files as well or did I misunderstand?
 
Ah I was hoping XF files would be left untouched, unless you're editing the files directly, I don't see why they need to be recached as well. Oh well I guess I'll just live with that, I'm probably making recaching files sound worse than it is. :unsure:
 
Well, you're free to use your own caching mechanism. Re-caching everything is much safer and easier for developers to use if they know they need it.
 
Just so there is no confusion what I meant was whether it would be better to continue using this for XF core JS files:
self::$jsVersion = substr(md5(self::$versionId . $config->jsVersion), 0, 8);

And for addons you could use the hashed $options->jsLastUpdate instead.

So you might have URLs like this:

js/xenforo/min/xenforo.js?_v=2ffed227 <-- core js file hash
js/xenforo/min/discussion.js?_v=2ffed227 <-- core js file hash
js/myaddon/file.js?_v=1de2ws3s <-- addon js file hash
js/anotheraddon/file.js?_v=1de2ws3s <-- addon js file hash

The core JS files shouldn't have much issue with caching since it'll change automatically when you upgrade. If you upgrade the addon that triggers the recache, then it'll only force addons to update instead of all XF files. Maybe I'm also missing that there are valid cases where the XF files need to be recached as well via an addon? Either way it's just a really minor issue that shouldn't be too much of a problem. :p I might end up keeping my own caching like you said, since it only changes for individual files, so I don't care anymore. :D
 
Top Bottom