Merge pull request #1504 from valadaptive/store-compiled-templates

Cache compiled Handlebars templates
This commit is contained in:
Cohee 2023-12-10 15:32:52 +02:00 committed by GitHub
commit 6be1c6ff10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -526,14 +526,17 @@ function getUrlSync(url, cache = true) {
}).responseText;
}
const templateCache = {};
const templateCache = new Map();
export function renderTemplate(templateId, templateData = {}, sanitize = true, localize = true, fullPath = false) {
try {
const pathToTemplate = fullPath ? templateId : `/scripts/templates/${templateId}.html`;
const templateContent = (pathToTemplate in templateCache) ? templateCache[pathToTemplate] : getUrlSync(pathToTemplate);
templateCache[pathToTemplate] = templateContent;
const template = Handlebars.compile(templateContent);
let template = templateCache.get(pathToTemplate);
if (!template) {
const templateContent = getUrlSync(pathToTemplate);
template = Handlebars.compile(templateContent);
templateCache.set(pathToTemplate, template);
}
let result = template(templateData);
if (sanitize) {