From: Jo-Philipp Wich Date: Tue, 9 Jul 2019 05:31:40 +0000 (+0200) Subject: luci-base: luci.js: cache determined RPC base url in session storage X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=66dfe64ca12c726571590a8552f4aea09a85ff9b;p=oweals%2Fluci.git luci-base: luci.js: cache determined RPC base url in session storage Signed-off-by: Jo-Philipp Wich --- diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 1bb4673b0..3e40b304b 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -210,8 +210,7 @@ }); - var requestQueue = [], - rpcBaseURL = null; + var requestQueue = []; function isQueueableRequest(opt) { if (!classes.rpc) @@ -223,8 +222,7 @@ 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); } @@ -544,6 +542,7 @@ var dummyElem = null, domParser = null, originalCBIInit = null, + rpcBaseURL = null, classes = {}; var LuCI = Class.extend({ @@ -581,7 +580,7 @@ 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; @@ -754,15 +753,40 @@ }, /* 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')