@cedgetec-utils/astro-components 1.10.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedgetec-utils/astro-components",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "keywords": [
5
5
  "astro-component"
6
6
  ],
@@ -25,21 +25,46 @@ const title =
25
25
  const ogTitle = slug === "" ? props.siteName : `${props.title}`;
26
26
 
27
27
  // Common favicon / PWA icon sizes.
28
- const faviconSizes = [16, 32, 48, 96, 192, 512];
29
-
30
- const buildIcons = (src: string | ImageMetadata) =>
31
- Promise.all(
32
- faviconSizes.map(async (size) => ({
33
- size,
34
- image: await getImage({
35
- src,
36
- width: size,
37
- height: size,
38
- fit: "contain",
39
- format: "png",
28
+ const faviconSizes = [32, 48, 96, 192, 512];
29
+
30
+ type Icon = { type: string; sizes: string; href: string };
31
+
32
+ const isSvg = (src: string | ImageMetadata) =>
33
+ typeof src === "string" ? src.endsWith(".svg") : src.format === "svg";
34
+
35
+ const buildSvgIcon = async (src: string | ImageMetadata): Promise<Icon[]> => {
36
+ const image = await getImage({ src, format: "svg", quality: "max" });
37
+ return [{ type: "image/svg+xml", sizes: "any", href: image.src }];
38
+ };
39
+
40
+ const buildIcons = async (src: string | ImageMetadata): Promise<Icon[]> => {
41
+ // An SVG favicon scales to any size, so pass it through unchanged rather
42
+ // than rasterizing it to PNG at every size.
43
+ if (isSvg(src)) {
44
+ return buildSvgIcon(src);
45
+ }
46
+
47
+ try {
48
+ return await Promise.all(
49
+ faviconSizes.map(async (size) => {
50
+ const image = await getImage({
51
+ src,
52
+ width: size,
53
+ height: size,
54
+ fit: "contain",
55
+ format: "png",
56
+ quality: "max",
57
+ });
58
+ return { type: "image/png", sizes: `${size}x${size}`, href: image.src };
40
59
  }),
41
- })),
42
- );
60
+ );
61
+ } catch {
62
+ // `isSvg` only detects SVGs by their `.svg` extension, so a string URL
63
+ // that points to an SVG without one slips through and fails to
64
+ // rasterize. Fall back to treating it as an SVG.
65
+ return buildSvgIcon(src);
66
+ }
67
+ };
43
68
 
44
69
  const iconsLight = await buildIcons(props.icons.light);
45
70
  const iconsDark = await buildIcons(props.icons.dark);
@@ -59,24 +84,19 @@ const openGraphImage = props.image
59
84
 
60
85
  {/* Default fallback icons (used when prefers-color-scheme is unsupported) */}
61
86
  {
62
- iconsDark.map(({ size, image }) => (
63
- <link
64
- rel="icon"
65
- type="image/png"
66
- sizes={`${size}x${size}`}
67
- href={image.src}
68
- />
87
+ iconsDark.map(({ type, sizes, href }) => (
88
+ <link rel="icon" type={type} sizes={sizes} href={href} />
69
89
  ))
70
90
  }
71
91
 
72
92
  {/* Light-colored icons for dark UI */}
73
93
  {
74
- iconsLight.map(({ size, image }) => (
94
+ iconsLight.map(({ type, sizes, href }) => (
75
95
  <link
76
96
  rel="icon"
77
- type="image/png"
78
- sizes={`${size}x${size}`}
79
- href={image.src}
97
+ type={type}
98
+ sizes={sizes}
99
+ href={href}
80
100
  media="(prefers-color-scheme:dark)"
81
101
  />
82
102
  ))
@@ -84,12 +104,12 @@ const openGraphImage = props.image
84
104
 
85
105
  {/* Dark-colored icons for light UI */}
86
106
  {
87
- iconsDark.map(({ size, image }) => (
107
+ iconsDark.map(({ type, sizes, href }) => (
88
108
  <link
89
109
  rel="icon"
90
- type="image/png"
91
- sizes={`${size}x${size}`}
92
- href={image.src}
110
+ type={type}
111
+ sizes={sizes}
112
+ href={href}
93
113
  media="(prefers-color-scheme:light)"
94
114
  />
95
115
  ))
@@ -115,4 +135,4 @@ const openGraphImage = props.image
115
135
  {props.type && <meta property="og:type" content={props.type} />}
116
136
  {openGraphImage && <meta property="og:image" content={openGraphImage.src} />}
117
137
  <slot />
118
- </head>
138
+ </head>