Skip to content

Commit

Permalink
cache already loaded links in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBLT committed Feb 11, 2024
1 parent fdeea08 commit 218f797
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/@hec.js/ui/lib/src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { f, setPropsOf, prop, hasProp, hasProps } from './props.js';
/** @type {{ [key: string]: Promise<HTMLTemplateElement> }} */
const templatesLoading = {}

/** @type {{ [key: string]: Promise<string> }} */
const resourcesLoading = {};

/**
* @param { string | URL } name
* @param { {[key: string]: any } } props
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 218f797

Please sign in to comment.