diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-07-09 07:31:40 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-07-10 07:15:02 +0200 |
commit | 66dfe64ca12c726571590a8552f4aea09a85ff9b (patch) | |
tree | c47a83b92e55099175e7a758f9f50ef03d2a317c | |
parent | ffdafd48009ae66c2bc97cabf4f038c120737e02 (diff) |
luci-base: luci.js: cache determined RPC base url in session storage
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 1bb4673b0a..3e40b304b6 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') |