From 218f7970df1a16a43c890657d4a49432e16c7025 Mon Sep 17 00:00:00 2001 From: Kevin Bulteel Date: Sun, 11 Feb 2024 21:31:46 +0100 Subject: [PATCH] cache already loaded links in templates --- packages/@hec.js/ui/lib/src/template.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/@hec.js/ui/lib/src/template.js b/packages/@hec.js/ui/lib/src/template.js index d9979cd..6f85bd4 100644 --- a/packages/@hec.js/ui/lib/src/template.js +++ b/packages/@hec.js/ui/lib/src/template.js @@ -7,6 +7,9 @@ import { f, setPropsOf, prop, hasProp, hasProps } from './props.js'; /** @type {{ [key: string]: Promise }} */ const templatesLoading = {} +/** @type {{ [key: string]: Promise }} */ +const resourcesLoading = {}; + /** * @param { string | URL } name * @param { {[key: string]: any } } props @@ -38,13 +41,17 @@ export function templateByName(name, props = {}) { cssLoads = []; for (const link of cssLinks) { - cssLoads.push(fetch(link.href, { headers: { 'accept': 'text/css' } }).then(r => r.text()).then((css) => { + resourcesLoading[link.href] ??= fetch(link.href, { headers: { 'accept': 'text/css' } }).then(r => r.text()); + + resourcesLoading[link.href].then((css) => { const style = document.createElement('style'); style.innerHTML = css; link.replaceWith(style); - })); + }); + + cssLoads.push(resourcesLoading[link.href]); } await Promise.all(cssLoads);