Skip to content

Commit

Permalink
fix(#15): fix parallelism issue with remote cache (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Dec 15, 2021
1 parent 8bd5d95 commit 9ff5e7d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-meals-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": patch
---

Fix parallelism issue when fetching icons from the Icon service in a map
31 changes: 31 additions & 0 deletions demo/src/pages/map.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
import { Icon } from 'astro-icon';
---

<html lang="en">

<head>
<meta charset="utf-8" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width" />
<title>Astro Icon</title>
<style lang="css">
[astro-icon] {
color: blue;
}
[astro-icon="annotation"] {
color: red;
}
[astro-icon="folder"] {
color: green;
}
</style>
</head>

<body>
<h1>Welcome to Astro Icon!</h1>

{[1,2,3].map(() => <Icon name="bi:stars" /> )}
</body>

</html>
34 changes: 18 additions & 16 deletions packages/core/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

1 comment on commit 9ff5e7d

@vercel
Copy link

@vercel vercel bot commented on 9ff5e7d Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.