From 7a076d41c8485d083b495e3cf06f1cb8e164f0b7 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 14 Dec 2021 22:22:19 -0600 Subject: [PATCH] fix(#15): fix parallelism issue with remote cache --- .changeset/wet-meals-share.md | 5 +++++ demo/src/pages/map.astro | 31 +++++++++++++++++++++++++++++++ packages/core/lib/resolver.ts | 34 ++++++++++++++++++---------------- 3 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 .changeset/wet-meals-share.md create mode 100644 demo/src/pages/map.astro diff --git a/.changeset/wet-meals-share.md b/.changeset/wet-meals-share.md new file mode 100644 index 00000000..669008f2 --- /dev/null +++ b/.changeset/wet-meals-share.md @@ -0,0 +1,5 @@ +--- +"astro-icon": patch +--- + +Fix parallelism issue when fetching icons from the Icon service in a map diff --git a/demo/src/pages/map.astro b/demo/src/pages/map.astro new file mode 100644 index 00000000..6acb96bb --- /dev/null +++ b/demo/src/pages/map.astro @@ -0,0 +1,31 @@ +--- +import { Icon } from 'astro-icon'; +--- + + + + + + + + Astro Icon + + + + +

Welcome to Astro Icon!

+ + {[1,2,3].map(() => )} + + + diff --git a/packages/core/lib/resolver.ts b/packages/core/lib/resolver.ts index 5a3e4177..48269be9 100644 --- a/packages/core/lib/resolver.ts +++ b/packages/core/lib/resolver.ts @@ -7,28 +7,30 @@ export default async function get(pack: string, name: string) { const url = new URL(`./${pack}/${name}`, baseURL).toString(); // Handle in-flight requests if (requests.has(url)) { - await requests.get(url); - return fetchCache.get(url); + return await requests.get(url); } - if (fetchCache.has(url)) { return fetchCache.get(url); } - let request = fetch(url); - requests.set(url, request); - const res = await request; - if (!res.ok) { - throw new Error(await res.text()); - } - const contentType = res.headers.get("Content-Type"); - if (!contentType.includes("svg")) { - throw new Error(`[astro-icon] Unable to load "${name}" because it did not resolve to an SVG! + + let request = async () => { + const res = await fetch(url); + if (!res.ok) { + throw new Error(await res.text()); + } + const contentType = res.headers.get("Content-Type"); + if (!contentType.includes("svg")) { + throw new Error(`[astro-icon] Unable to load "${name}" because it did not resolve to an SVG! Recieved the following "Content-Type": ${contentType}`); + } + const svg = await res.text(); + fetchCache.set(url, svg); + requests.delete(url); + return svg; } - const svg = await res.text(); - fetchCache.set(url, svg); - requests.delete(url); - return svg; + let promise = request(); + requests.set(url, promise); + return await promise; }