mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 15:05:46 +01:00
added caching per url + rate limiting mechanism to prefetcher, closes #1576
This commit is contained in:
@@ -9,7 +9,9 @@ var ajaxify = ajaxify || {};
|
|||||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
|
||||||
templatesConfig = null,
|
templatesConfig = null,
|
||||||
availableTemplates = null,
|
availableTemplates = null,
|
||||||
apiXHR = null;
|
apiXHR = null,
|
||||||
|
|
||||||
|
PRELOADER_RATE_LIMIT = 10000;
|
||||||
|
|
||||||
window.onpopstate = function (event) {
|
window.onpopstate = function (event) {
|
||||||
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
|
if (event !== null && event.state && event.state.url !== undefined && !ajaxify.initialLoad) {
|
||||||
@@ -21,7 +23,7 @@ var ajaxify = ajaxify || {};
|
|||||||
|
|
||||||
ajaxify.currentPage = null;
|
ajaxify.currentPage = null;
|
||||||
ajaxify.initialLoad = false;
|
ajaxify.initialLoad = false;
|
||||||
ajaxify.preloader = null;
|
ajaxify.preloader = {};
|
||||||
|
|
||||||
function onAjaxError(err) {
|
function onAjaxError(err) {
|
||||||
var data = err.data, textStatus = err.textStatus;
|
var data = err.data, textStatus = err.textStatus;
|
||||||
@@ -181,8 +183,8 @@ var ajaxify = ajaxify || {};
|
|||||||
ajaxify.loadData = function(url, callback) {
|
ajaxify.loadData = function(url, callback) {
|
||||||
$(window).trigger('action:ajaxify.loadingData', {url: url});
|
$(window).trigger('action:ajaxify.loadingData', {url: url});
|
||||||
|
|
||||||
if (ajaxify.preloader && ajaxify.preloader.url === url) {
|
if (ajaxify.preloader && ajaxify.preloader[url]) {
|
||||||
return callback(null, ajaxify.preloader.data);
|
return callback(null, ajaxify.preloader[url].data);
|
||||||
}
|
}
|
||||||
|
|
||||||
var location = document.location || window.location,
|
var location = document.location || window.location,
|
||||||
@@ -291,24 +293,23 @@ var ajaxify = ajaxify || {};
|
|||||||
|
|
||||||
if (this.host === window.location.host) {
|
if (this.host === window.location.host) {
|
||||||
// Internal link
|
// Internal link
|
||||||
var url = this.href.replace(rootUrl + '/', '');
|
var url = this.href.replace(rootUrl + '/', ''),
|
||||||
|
currentTime = (new Date()).getTime();
|
||||||
ajaxify.loadData(url, function(err, data) {
|
|
||||||
ajaxify.preloader = {
|
if (!ajaxify.preloader[url] || currentTime - ajaxify.preloader[url].lastFetched > PRELOADER_RATE_LIMIT) {
|
||||||
url: url,
|
ajaxify.preloader[url] = null;
|
||||||
data: data
|
ajaxify.loadData(url, function(err, data) {
|
||||||
};
|
ajaxify.preloader[url] = {
|
||||||
});
|
url: url,
|
||||||
|
data: data,
|
||||||
|
lastFetched: currentTime
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$(document.body).on('mouseout', 'a', function (e) {
|
|
||||||
setTimeout(function() {
|
|
||||||
ajaxify.preloader = null;
|
|
||||||
}, 500);
|
|
||||||
});
|
|
||||||
|
|
||||||
templates.registerLoader(ajaxify.loadTemplate);
|
templates.registerLoader(ajaxify.loadTemplate);
|
||||||
|
|
||||||
$.when($.getJSON(RELATIVE_PATH + '/templates/config.json'), $.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
|
$.when($.getJSON(RELATIVE_PATH + '/templates/config.json'), $.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user