luci-base: luci.js: append version when dynamically loading classes
authorJo-Philipp Wich <jo@mein.io>
Sat, 14 Sep 2019 15:44:32 +0000 (17:44 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sat, 14 Sep 2019 15:44:32 +0000 (17:44 +0200)
This allows for some naive cache busting when the LuCI version changes,
to avoid loading outdated classes from cache.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/luci.js

index 687ac0e678646b77cbe6eb27425ce2d17689fe87..bcc6870bd218e4cfa36c313df82690343cb454f4 100644 (file)
                __init__: function(env) {
 
                        document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) {
-                               if (env.base_url == null || env.base_url == '')
-                                       env.base_url = s.getAttribute('src').replace(/\/luci\.js(?:\?v=[^?]+)?$/, '');
+                               if (env.base_url == null || env.base_url == '') {
+                                       var m = (s.getAttribute('src') || '').match(/^(.*)\/luci\.js(?:\?v=([^?]+))?$/);
+                                       if (m) {
+                                               env.base_url = m[1];
+                                               env.resource_version = m[2];
+                                       }
+                               }
                        });
 
                        if (env.base_url == null)
                                return classes[name];
                        }
 
-                       url = '%s/%s.js'.format(L.env.base_url, name.replace(/\./g, '/'));
+                       url = '%s/%s.js%s'.format(L.env.base_url, name.replace(/\./g, '/'), (L.env.resource_version ? '?v=' + L.env.resource_version : ''));
                        from = [ name ].concat(from);
 
                        var compileClass = function(res) {