@all-the.rest/astro-image-cdn 0.5.0 → 0.5.1

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/README.md CHANGED
@@ -60,7 +60,14 @@ import photo from "../assets/photo.jpg";
60
60
  <CdnImage src={photo} alt="A landscape" sizes="(max-width: 600px) 100vw, 50vw" />
61
61
  ```
62
62
 
63
- Its entire prop surface is `src` (an imported local image via Astro assets), `alt`, and optional `sizes`; there are no per-image width, format, quality, effort, or `baseUrl` props. Widths, formats and their order, quality, encoder efforts, `baseUrl`, and the dev/build URL mapping all come from the integration configuration. The component emits no styles and renders only `<picture>` with one `<source>` per configured format (AVIF then WebP by default) followed by an `<img>` fallback in the last configured format. Project-relative string paths are not supported because Astro's public image APIs require imports; import the image instead. The integration must be active (not `devOnly` during a build).
63
+ Its props are `src` (an imported local image via Astro assets), `alt`, optional `sizes`, optional `pictureClass`, and the official `<img>` attributes typed via Astro's `HTMLAttributes<"img">`. Those attributes — `class`, `loading`, `decoding`, `fetchpriority`, `title`, `data-*`, `aria-*` — and any other unknown prop are forwarded to the rendered `<img>` only, while `pictureClass` goes to the `<picture>` wrapper — for example `class="contents"` so a grid or overlay layout can position the wrapper. `src`, `srcset`, `width`, `height` and inline `style` stay reserved for the pipeline, and the component props `alt`, `sizes` and `pictureClass` are never forwarded from the caller. This lets a site-specific wrapper pass layout classes or lightbox/EXIF metadata without reimplementing the responsive markup:
64
+
65
+ ```astro
66
+ <CdnImage src={photo} alt="A landscape" sizes="100vw" pictureClass="contents" class="w-full"
67
+ loading="eager" data-pswp-width={1200} data-pswp-height={800} />
68
+ ```
69
+
70
+ There are no per-image width, format, quality, effort, or `baseUrl` props. Widths, formats and their order, quality, encoder efforts, `baseUrl`, and the dev/build URL mapping all come from the integration configuration. Pipeline prop names (`widths`, `formats`, `quality`, `webpEffort`, `avifEffort`, `baseUrl`) and the reserved attributes `src`, `srcSet`, `width`, `height`, `style`, `alt`, `sizes` and `pictureClass` are ignored when a caller passes them — case-insensitively — even though the component's `[key: string]: unknown` index signature accepts them at the type level. The component emits no styles and renders only `<picture>` with one `<source>` per configured format (AVIF then WebP by default) followed by an `<img>` fallback in the last configured format. Project-relative string paths are not supported because Astro's public image APIs require imports; import the image instead. The integration must be active (not `devOnly` during a build).
64
71
 
65
72
  For a locally linked package that should affect development only, set `devOnly: true`:
66
73
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@all-the.rest/astro-image-cdn",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Persistent, incremental image variants for Astro's native image components",
5
5
  "type": "module",
6
6
  "license": "ISC",
@@ -1,18 +1,30 @@
1
1
  ---
2
2
  import { getImage, imageConfig } from "astro:assets";
3
+ import type { HTMLAttributes } from "astro/types";
3
4
 
4
- export interface Props {
5
+ /** Official <img> attributes minus the pipeline-derived ones. */
6
+ export type Props = Omit<
7
+ HTMLAttributes<"img">,
8
+ "src" | "srcset" | "width" | "height" | "style"
9
+ > & {
10
+ /** Imported local image (Astro ImageMetadata). */
5
11
  src: import("astro").ImageMetadata;
12
+ /** Alternative text. */
6
13
  alt: string;
14
+ /** Optional HTML sizes hint; forwarded to <source> and <img>. */
7
15
  sizes?: string;
8
- }
16
+ /** Optional class for the <picture> wrapper (layout only). */
17
+ pictureClass?: string;
18
+ /** Any other prop is forwarded as an HTML attribute to the <img>. */
19
+ [key: string]: unknown;
20
+ };
9
21
 
10
22
  const config = imageConfig.service.config;
11
23
  if (!config || typeof config.rootDir !== "string") {
12
24
  throw new Error("CdnImage requires the astroImageCdn integration to be enabled in the Astro config.");
13
25
  }
14
26
 
15
- const { src, alt, sizes } = Astro.props;
27
+ const { src, alt, sizes, pictureClass, ...forwarded } = Astro.props;
16
28
  if (alt === undefined || alt === null) {
17
29
  throw new TypeError("CdnImage requires an alt attribute.");
18
30
  }
@@ -27,11 +39,25 @@ const optimized = await Promise.all(
27
39
  formats.map((format) => getImage({ ...base, format, layout: "none" })),
28
40
  );
29
41
  const fallback = optimized.at(-1);
42
+
43
+ // Merge into one object: spreading twice would emit duplicate HTML attributes,
44
+ // and browsers use the first value instead of the caller's override.
45
+ const imgAttributes: Record<string, unknown> = { ...fallback.attributes };
46
+ // Pipeline-derived attributes, component-only props and pipeline prop names never
47
+ // reach the <img>. Keys are compared lowercased so camelCase variants such as
48
+ // `srcSet` cannot add a second attribute next to the pipeline's own.
49
+ const reservedAttributes = new Set([
50
+ "src", "srcset", "width", "height", "style", "alt", "sizes", "pictureclass", "classlist",
51
+ "widths", "formats", "quality", "webpeffort", "avifeffort", "baseurl",
52
+ ]);
53
+ for (const [key, value] of Object.entries(forwarded)) {
54
+ if (!reservedAttributes.has(key.toLowerCase())) imgAttributes[key] = value;
55
+ }
30
56
  ---
31
57
 
32
- <picture>
58
+ <picture {...(pictureClass ? { class: pictureClass } : {})}>
33
59
  {optimized.map((img, i) => (
34
60
  <source srcset={img.srcSet.attribute || img.src} type={`image/${img.options.format ?? formats[i]}`} {...(sizes ? { sizes } : {})} />
35
61
  ))}
36
- <img src={fallback.src} {...(fallback.srcSet.attribute ? { srcset: fallback.srcSet.attribute } : {})} {...fallback.attributes} />
62
+ <img src={fallback.src} {...(fallback.srcSet.attribute ? { srcset: fallback.srcSet.attribute } : {})} {...imgAttributes} />
37
63
  </picture>
@@ -1,14 +1,23 @@
1
1
  import type { AstroComponentFactory } from "astro/runtime/server/index.js";
2
2
  import type { ImageMetadata } from "astro";
3
+ import type { HTMLAttributes } from "astro/types";
3
4
 
4
- export interface CdnImageProps {
5
+ /** Official <img> attributes minus the pipeline-derived ones. */
6
+ export type CdnImageProps = Omit<
7
+ HTMLAttributes<"img">,
8
+ "src" | "srcset" | "width" | "height" | "style"
9
+ > & {
5
10
  /** Imported local image (Astro ImageMetadata). */
6
11
  src: ImageMetadata;
7
12
  /** Alternative text. */
8
13
  alt: string;
9
14
  /** Optional HTML sizes hint; forwarded to <source> and <img>. */
10
15
  sizes?: string;
11
- }
16
+ /** Optional class for the <picture> wrapper (layout only). */
17
+ pictureClass?: string;
18
+ /** Any other prop is forwarded as an HTML attribute to the <img>. */
19
+ [key: string]: unknown;
20
+ };
12
21
 
13
22
  declare const CdnImage: AstroComponentFactory & ((props: CdnImageProps) => unknown);
14
23
  export default CdnImage;