Skip to content
Snippets Groups Projects
Commit de259f45 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Add icon request caching

We now only create one request per icon.
parent 675f70e3
Branches
No related tags found
No related merge requests found
...@@ -95,7 +95,8 @@ class VpuCommonDemo extends VPULitElement { ...@@ -95,7 +95,8 @@ class VpuCommonDemo extends VPULitElement {
<p style="font-size: 2em; color: orange">Foo <vpu-icon name="cloudnetwork"></vpu-icon> bar</p> <p style="font-size: 2em; color: orange">Foo <vpu-icon name="cloudnetwork"></vpu-icon> bar</p>
<span style="background-color: #000"><a href="#" style=" color: #fff">foobar</a></span> <span style="background-color: #000"><a href="#" style=" color: #fff">foobar</a></span>
<br> <br>
<vpu-icon style="color: green; height: 50px; border: #000 solid 1px"></vpu-icon>
${(new Array(50).fill(0)).map(i => html`<vpu-icon style="color: green; width: 50px; height: 50px; border: #000 solid 1px"name="coffee-cup"></vpu-icon>`)}
</div> </div>
</div> </div>
<div class="container"> <div class="container">
......
...@@ -41,6 +41,7 @@ export function getIconCSS(name) { ...@@ -41,6 +41,7 @@ export function getIconCSS(name) {
async function getSVGTextElement(name) { async function getSVGTextElement(name) {
const iconURL = getIconSVGURL(name); const iconURL = getIconSVGURL(name);
const response = await fetch(iconURL); const response = await fetch(iconURL);
if (!response.ok) { if (!response.ok) {
return unsafeHTML(errorIcon); return unsafeHTML(errorIcon);
...@@ -53,10 +54,28 @@ async function getSVGTextElement(name) { ...@@ -53,10 +54,28 @@ async function getSVGTextElement(name) {
return unsafeHTML(text); return unsafeHTML(text);
} }
const iconCache = {};
/**
* Avoid lots of requests in case an icon is used multiple times on the same page.
*
* @param {string} name
*/
async function getSVGTextElementCached(name) {
if (iconCache[name] === undefined) {
let promise = getSVGTextElement(name);
iconCache[name] = promise;
return promise;
} else {
return iconCache[name];
}
}
/** /**
* For icon names see https://materialdesignicons.com/ * For icon names see https://lineicons.com
*/ */
class Icon extends LitElement { class Icon extends LitElement {
constructor() { constructor() {
super(); super();
this.name = "bolt"; this.name = "bolt";
...@@ -88,7 +107,7 @@ class Icon extends LitElement { ...@@ -88,7 +107,7 @@ class Icon extends LitElement {
} }
render() { render() {
let svg = getSVGTextElement(this.name); let svg = getSVGTextElementCached(this.name);
return html` return html`
${until(svg)} ${until(svg)}
`; `;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment