@all-the.rest/astro-image-cdn 0.4.1 → 0.5.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/README.md +15 -0
- package/package.json +5 -1
- package/src/CdnImage.astro +37 -0
- package/src/cdn-image.d.ts +14 -0
package/README.md
CHANGED
|
@@ -47,6 +47,21 @@ const socialImage = await getImage({ src: photo, width: 1200, format: "webp" });
|
|
|
47
47
|
|
|
48
48
|
In production builds, `Image`, `Picture`, and `getImage()` generate absolute URLs under `baseUrl`. In `astro dev`, requested variants are served directly from `outputDir` at `/_astro-image-cdn/`; the URL is local and does not require a CDN upload. The integration does not upload assets, configure a CDN, deploy, or modify hosting configuration. Production output must be made available at the corresponding `baseUrl` by the site's separate publishing process.
|
|
49
49
|
|
|
50
|
+
### Optional `CdnImage` component
|
|
51
|
+
|
|
52
|
+
Native `<Picture>` remains the default and is the best choice when you want Astro's responsive layout/style features and per-image control. For sites rendering images by slug/content, or that want CDN-correct markup without maintaining their own glue, the package also exports an opt-in component:
|
|
53
|
+
|
|
54
|
+
```astro
|
|
55
|
+
---
|
|
56
|
+
import CdnImage from "@all-the.rest/astro-image-cdn/CdnImage";
|
|
57
|
+
import photo from "../assets/photo.jpg";
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
<CdnImage src={photo} alt="A landscape" sizes="(max-width: 600px) 100vw, 50vw" />
|
|
61
|
+
```
|
|
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).
|
|
64
|
+
|
|
50
65
|
For a locally linked package that should affect development only, set `devOnly: true`:
|
|
51
66
|
|
|
52
67
|
```js
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@all-the.rest/astro-image-cdn",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Persistent, incremental image variants for Astro's native image components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"types": "./src/index.d.ts",
|
|
21
21
|
"import": "./src/index.js"
|
|
22
22
|
},
|
|
23
|
+
"./CdnImage": {
|
|
24
|
+
"types": "./src/cdn-image.d.ts",
|
|
25
|
+
"import": "./src/CdnImage.astro"
|
|
26
|
+
},
|
|
23
27
|
"./service": "./src/service.js"
|
|
24
28
|
},
|
|
25
29
|
"engines": {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { getImage, imageConfig } from "astro:assets";
|
|
3
|
+
|
|
4
|
+
export interface Props {
|
|
5
|
+
src: import("astro").ImageMetadata;
|
|
6
|
+
alt: string;
|
|
7
|
+
sizes?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const config = imageConfig.service.config;
|
|
11
|
+
if (!config || typeof config.rootDir !== "string") {
|
|
12
|
+
throw new Error("CdnImage requires the astroImageCdn integration to be enabled in the Astro config.");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const { src, alt, sizes } = Astro.props;
|
|
16
|
+
if (alt === undefined || alt === null) {
|
|
17
|
+
throw new TypeError("CdnImage requires an alt attribute.");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const formats = config.formats;
|
|
21
|
+
if (!Array.isArray(formats) || formats.length === 0) {
|
|
22
|
+
throw new Error("CdnImage requires at least one configured format from the astroImageCdn integration.");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const base = { src, alt, widths: config.widths, ...(sizes ? { sizes } : {}) };
|
|
26
|
+
const optimized = await Promise.all(
|
|
27
|
+
formats.map((format) => getImage({ ...base, format, layout: "none" })),
|
|
28
|
+
);
|
|
29
|
+
const fallback = optimized.at(-1);
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
<picture>
|
|
33
|
+
{optimized.map((img, i) => (
|
|
34
|
+
<source srcset={img.srcSet.attribute || img.src} type={`image/${img.options.format ?? formats[i]}`} {...(sizes ? { sizes } : {})} />
|
|
35
|
+
))}
|
|
36
|
+
<img src={fallback.src} {...(fallback.srcSet.attribute ? { srcset: fallback.srcSet.attribute } : {})} {...fallback.attributes} />
|
|
37
|
+
</picture>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AstroComponentFactory } from "astro/runtime/server/index.js";
|
|
2
|
+
import type { ImageMetadata } from "astro";
|
|
3
|
+
|
|
4
|
+
export interface CdnImageProps {
|
|
5
|
+
/** Imported local image (Astro ImageMetadata). */
|
|
6
|
+
src: ImageMetadata;
|
|
7
|
+
/** Alternative text. */
|
|
8
|
+
alt: string;
|
|
9
|
+
/** Optional HTML sizes hint; forwarded to <source> and <img>. */
|
|
10
|
+
sizes?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare const CdnImage: AstroComponentFactory & ((props: CdnImageProps) => unknown);
|
|
14
|
+
export default CdnImage;
|