Use XHR to load HTML templates

This commit is contained in:
Cohee 2024-01-12 22:00:08 +02:00
parent 4fe13fab8e
commit 6086cedf2b
1 changed files with 16 additions and 7 deletions

View File

@ -533,13 +533,22 @@ $(document).ajaxError(function myErrorHandler(_, xhr) {
}
});
function getUrlSync(url, cache = true) {
return $.ajax({
type: 'GET',
url: url,
cache: cache,
async: false,
}).responseText;
/**
* Loads a URL content using XMLHttpRequest synchronously.
* @param {string} url URL to load synchronously
* @returns {string} Response text
*/
function getUrlSync(url) {
console.debug('Loading URL synchronously', url);
const request = new XMLHttpRequest();
request.open('GET', url, false); // `false` makes the request synchronous
request.send();
if (request.status >= 200 && request.status < 300) {
return request.responseText;
}
throw new Error(`Error loading ${url}: ${request.status} ${request.statusText}`);
}
const templateCache = new Map();