@cedgetec-utils/astro-components 1.12.0 → 1.13.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.12.0",
3
+ "version": "1.13.0",
4
4
  "keywords": [
5
5
  "astro-component"
6
6
  ],
@@ -29,52 +29,57 @@ const faviconSizes = [32, 48, 96, 192, 512];
29
29
 
30
30
  type Icon = { type: string; sizes: string; href: string };
31
31
 
32
- const isSvg = (src: string | ImageMetadata) =>
33
- typeof src === "string" ? src.endsWith(".svg") : src.format === "svg";
32
+ const isSvg = async (src: string | ImageMetadata): Promise<boolean> => {
33
+ if (typeof src !== "string") return src.format === "svg";
34
+ if (src.endsWith(".svg")) return true;
35
+
36
+ // Remote / extensionless URLs (e.g. Directus assets) carry no hint in the
37
+ // path, so probe the response's content type. `getImage` can't tell us:
38
+ // it only builds the `/_image` URL — the rasterization (and any
39
+ // UnsupportedImageFormat error) happens later when that URL is requested,
40
+ // outside this component, so we must detect SVGs up front.
41
+ try {
42
+ const res = await fetch(src, { method: "HEAD" });
43
+ return (res.headers.get("content-type") ?? "").includes("svg");
44
+ } catch {
45
+ return false;
46
+ }
47
+ };
34
48
 
35
49
  const buildSvgIcon = async (src: string | ImageMetadata): Promise<Icon[]> => {
36
- const image = await getImage({ src, format: "svg", quality: "max" });
50
+ const image = await getImage({ src, format: "svg", inferSize: true});
37
51
  return [{ type: "image/svg+xml", sizes: "any", href: image.src }];
38
52
  };
39
53
 
40
54
  const buildIcons = async (src: string | ImageMetadata): Promise<Icon[]> => {
41
55
  // An SVG favicon scales to any size, so pass it through unchanged rather
42
56
  // than rasterizing it to PNG at every size.
43
- if (isSvg(src)) {
57
+ if (await isSvg(src)) {
44
58
  return buildSvgIcon(src);
45
59
  }
46
60
 
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 };
59
- }),
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
- }
61
+ return Promise.all(
62
+ faviconSizes.map(async (size) => {
63
+ const image = await getImage({
64
+ src,
65
+ width: size,
66
+ height: size,
67
+ fit: "contain",
68
+ format: "png",
69
+ quality: "max",
70
+ });
71
+ return { type: "image/png", sizes: `${size}x${size}`, href: image.src };
72
+ }),
73
+ );
67
74
  };
68
75
 
69
76
  const iconsLight = await buildIcons(props.icons.light);
70
77
  const iconsDark = await buildIcons(props.icons.dark);
71
78
 
72
79
  const openGraphImage = props.image
73
- ? await getImage({
74
- src: props.image,
75
- width: 1000,
76
- height: 500,
77
- })
80
+ ? (await isSvg(props.image))
81
+ ? await getImage({ src: props.image, format: "svg", quality: "max" })
82
+ : await getImage({ src: props.image, width: 1000, height: 500 })
78
83
  : null;
79
84
  ---
80
85