luci-base: luci.js: cache determined RPC base url in session storage
authorJo-Philipp Wich <jo@mein.io>
Tue, 9 Jul 2019 05:31:40 +0000 (07:31 +0200)
committerJo-Philipp Wich <jo@mein.io>
Wed, 10 Jul 2019 05:15:02 +0000 (07:15 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/luci.js

index 1bb4673b0af54efa88c8d66179d1809437b53181..3e40b304b61c4cafebb36ce635dd55ab67bf087a 100644 (file)
        });
 
 
-       var requestQueue = [],
-           rpcBaseURL = null;
+       var requestQueue = [];
 
        function isQueueableRequest(opt) {
                if (!classes.rpc)
                if (opt.nobatch === true)
                        return false;
 
-               if (rpcBaseURL == null)
-                       rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL());
+               var rpcBaseURL = Request.expandURL(classes.rpc.getBaseURL());
 
                return (rpcBaseURL != null && opt.url.indexOf(rpcBaseURL) == 0);
        }
        var dummyElem = null,
            domParser = null,
            originalCBIInit = null,
+           rpcBaseURL = null,
            classes = {};
 
        var LuCI = Class.extend({
                                this.require('ui'),
                                this.require('rpc'),
                                this.require('form'),
-                               Request.get('/ubus/').catch(function() { return { status: 0 } })
+                               this.probeRPCBaseURL()
                        ]).then(this.setupDOM.bind(this)).catch(this.error);
 
                        originalCBIInit = window.cbi_init;
                },
 
                /* DOM setup */
+               probeRPCBaseURL: function() {
+                       if (rpcBaseURL == null) {
+                               try {
+                                       rpcBaseURL = window.sessionStorage.getItem('rpcBaseURL');
+                               }
+                               catch (e) { }
+                       }
+
+                       if (rpcBaseURL == null) {
+                               rpcBaseURL = Request.get('/ubus/').then(function(res) {
+                                       return (rpcBaseURL = (res.status == 400) ? '/ubus/' : this.url('admin/ubus'));
+                               }, function() {
+                                       return (rpcBaseURL = L.url('admin/ubus'));
+                               }).then(function(url) {
+                                       try {
+                                               window.sessionStorage.setItem('rpcBaseURL', url);
+                                       }
+                                       catch (e) { }
+
+                                       return url;
+                               });
+                       }
+
+                       return Promise.resolve(rpcBaseURL);
+               },
+
                setupDOM: function(res) {
                        var domEv = res[0],
                            uiClass = res[1],
                            rpcClass = res[2],
                            formClass = res[3],
-                           ubusReply = res[4];
+                           rpcBaseURL = res[4];
 
-                       if (ubusReply.status == 400)
-                               rpcClass.setBaseURL('/ubus/');
+                       rpcClass.setBaseURL(rpcBaseURL);
 
                        Request.addInterceptor(function(res) {
                                if (res.status != 403 || res.headers.get('X-LuCI-Login-Required') != 'yes')