Skip to content

Commit

Permalink
improve external link component
Browse files Browse the repository at this point in the history
  • Loading branch information
Trombach committed Jan 1, 2025
1 parent 521d88c commit 1ab2426
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"sharp": "^0.33.5",
"svelte": "^5.16.0",
"tailwindcss": "^3.4.17",
"uint8array-extras": "^1.4.0",
"unplugin-icons": "^0.22.0"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions src/components/ExternalLink.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import type { HTMLAttributes } from "astro/types";
import { uint8ArrayToBase64 } from "uint8array-extras";
const FAVICON_API = new URL("https://www.google.com/s2/favicons");
Expand All @@ -9,7 +10,7 @@ type Props = HTMLAttributes<"a"> & {
const { href, position = "before", ...props } = Astro.props;
let favicon: Buffer | undefined = undefined;
let favicon: Uint8Array | undefined = undefined;
if (
href &&
(typeof href === "string"
Expand All @@ -20,25 +21,26 @@ if (
const searchParams = new URLSearchParams({ domain: url.hostname, sz: "64" });
try {
favicon = await fetch(`${FAVICON_API}?${searchParams}`).then(async (res) =>
Buffer.from(await res.arrayBuffer()),
);
const response = await fetch(`${FAVICON_API}?${searchParams}`);
favicon = await response.bytes();
} catch (err) {
console.error(`Error fetching favicon: ${href}`, err);
console.warn(`Error fetching favicon: ${href}`, err);
}
}
const content = (await Astro.slots.render("default")).trim();
const imageProps = {
width: 16,
height: 16,
src: `data:image/png;base64,${favicon?.toString("base64")}`,
alt: "favicon",
fetchpriority: "low",
loading: "lazy",
decoding: "async",
"data-site-icon": true,
} as const satisfies HTMLAttributes<"img">;
const imageProps =
favicon &&
({
width: 16,
height: 16,
src: `data:image/png;base64,${uint8ArrayToBase64(favicon)}`,
alt: "favicon",
fetchpriority: "low",
loading: "lazy",
decoding: "async",
"aria-hidden": true,
} as const satisfies HTMLAttributes<"img">);
---

{
Expand Down

0 comments on commit 1ab2426

Please sign in to comment.