Create client-side javascript cache-busting system for API
This commit is contained in:
parent
f0c027e178
commit
d197565fcc
|
@ -23,6 +23,11 @@ window.consts = {
|
|||
};
|
||||
window.view = {};
|
||||
window.cache = {};
|
||||
window.http_cachebuster = {
|
||||
'api/v1/http/[^\/]+/accounts/[^\/]+': 'users',
|
||||
'api/v1/config/trusted_users/add': 'users',
|
||||
'api/v1/cache/put?keys=temp_hidden': 'users',
|
||||
};
|
||||
</script>
|
||||
<script>
|
||||
function apcontrol_title(str) {
|
||||
|
|
58
js/base.php
58
js/base.php
|
@ -328,16 +328,60 @@ const http = {
|
|||
}
|
||||
E.custom.loader.hide();
|
||||
},
|
||||
request: function(method, path, payload, callbk) {
|
||||
cache: {
|
||||
buster_process: function(path) {
|
||||
if (window.http_cachebuster === undefined)
|
||||
return false;
|
||||
const ks = Object.keys(window.http_cachebuster);
|
||||
var keys = null;
|
||||
for (var i = 0; i < ks.length; i++) {
|
||||
if (path.match(new RegExp(ks[i])) || path.startsWith(ks[i])) {
|
||||
keys = window.http_cachebuster[ks[i]];
|
||||
keys = keys.trim().split('|');
|
||||
for (var j = 0; j < keys.length; j++)
|
||||
keys[j] = keys[j].trim();
|
||||
}
|
||||
}
|
||||
if (keys === null)
|
||||
return false;
|
||||
for (var i = 0; i < keys.length; i++)
|
||||
http.cache.bust(keys[i]);
|
||||
},
|
||||
bust: function(key) {
|
||||
localStorage['http_cachekey__'+key] = (new Date()).getTime();
|
||||
},
|
||||
bust_if_new: function(key) {
|
||||
if (localStorage['http_cachekey__'+key] === undefined)
|
||||
http.cache.bust(key);
|
||||
},
|
||||
get: function(key) {
|
||||
return localStorage['http_cachekey__'+key] || (new Date()).getTime();
|
||||
},
|
||||
},
|
||||
request: function(method, path, payload, callbk, cache_key) {
|
||||
cache_key = cache_key || null;
|
||||
payload = payload || null;
|
||||
callbk = callbk || null;
|
||||
|
||||
//console.log(path); printstack();
|
||||
const httpid = uuidv4();
|
||||
const httpdiv = document.getElementById('http');
|
||||
const httpts = new Date().getTime();
|
||||
if (httpdiv !== null)
|
||||
httpdiv.innerHTML += '<div id="http-'+httpts+'">'+method+' '+path+'</div>';
|
||||
|
||||
document.activeElement.blur();
|
||||
payload = payload || null;
|
||||
callbk = callbk || null;
|
||||
http.cache.buster_process(path);
|
||||
var cachestr = '';
|
||||
if (cache_key !== null) {
|
||||
http.cache.bust_if_new(cache_key);
|
||||
var cacheval = http.cache.get(cache_key);
|
||||
if (!path.includes('?'))
|
||||
cachestr = '?_c='+cacheval;
|
||||
else cachestr = '&_c='+cacheval;
|
||||
}
|
||||
path += cachestr;
|
||||
|
||||
const oReq = new XMLHttpRequest();
|
||||
oReq.addEventListener("load", function() {
|
||||
delete window.http_requests[httpid];
|
||||
|
@ -368,11 +412,11 @@ const http = {
|
|||
oReq.send(JSON_to_URLEncoded(payload));
|
||||
window.http_requests[httpid] = oReq;
|
||||
},
|
||||
get: function(path, payload, callbk) {
|
||||
return http.request('GET', path, payload, callbk);
|
||||
get: function(path, payload, callbk, cache_key) {
|
||||
return http.request('GET', path, payload, callbk, cache_key);
|
||||
},
|
||||
post: function(path, payload, callbk) {
|
||||
return http.request('POST', path, payload, callbk);
|
||||
post: function(path, payload, callbk, cache_key) {
|
||||
return http.request('POST', path, payload, callbk, cache_key);
|
||||
},
|
||||
API: {
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ window.view.instance = {
|
|||
if (!confirm('Are you sure you want to suspend this user?')) return false;
|
||||
const hargs = get_hash_arguments();
|
||||
|
||||
http.get(`api/v1/http/mastodon/accounts/suspend?instance=${hargs.instance}&id=${_id}`,
|
||||
http.get(`api/v1/http/${hargs.software}/accounts/suspend?instance=${hargs.instance}&id=${_id}`,
|
||||
{}, function(js) {
|
||||
toast.info('User has been successfully suspended');
|
||||
});
|
||||
|
@ -149,7 +149,7 @@ window.view.instance = {
|
|||
silence: function(_id) {
|
||||
const hargs = get_hash_arguments();
|
||||
|
||||
http.get(`api/v1/http/mastodon/accounts/silence?instance=${hargs.instance}&id=${_id}`,
|
||||
http.get(`api/v1/http/${hargs.software}/accounts/silence?instance=${hargs.instance}&id=${_id}`,
|
||||
{}, function(js) {
|
||||
toast.info('User has been successfully silenced');
|
||||
});
|
||||
|
@ -401,7 +401,7 @@ window.view.instance = {
|
|||
E.template('#paging.bottom', function(TPL) {
|
||||
return E.elemid('paging').innerHTML;
|
||||
});
|
||||
});
|
||||
}, 'users');
|
||||
},
|
||||
save_filter: function() {
|
||||
const data = window.view.instance.do.filter_users.get_current_filter();
|
||||
|
|
Loading…
Reference in New Issue