@tokenoftrust/storefront-runner 6.11.6 → 6.11.7

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.
@@ -14,7 +14,12 @@
14
14
  * the storefront is served by Cloudflare or CloudFront, so this component never
15
15
  * routes them through a provider-specific transformation path.
16
16
  */
17
- import { resolveImage } from "@/lib/assets/shared/imageAttrs";
17
+ import {
18
+ isCapturedPoolRaster,
19
+ resolveImage,
20
+ type CapturedImageVariant,
21
+ type CapturedVariantManifest,
22
+ } from "@/lib/assets/shared/imageAttrs";
18
23
 
19
24
  interface Props {
20
25
  src?: string;
@@ -26,6 +31,10 @@ interface Props {
26
31
  /** Short label drawn into the placeholder (e.g. product title). */
27
32
  label?: string;
28
33
  rehosted?: boolean;
34
+ /** Captured width variants from the revision manifest, when present. */
35
+ variants?: readonly CapturedImageVariant[];
36
+ /** Optional manifest mapping source URLs or `/_a/<address>` entries to width variants. */
37
+ manifest?: CapturedVariantManifest | null;
29
38
  /** LCP hint: eager + high priority + no lazy. */
30
39
  priority?: boolean;
31
40
  sizes?: string;
@@ -41,15 +50,30 @@ const {
41
50
  seed,
42
51
  label,
43
52
  rehosted = false,
53
+ variants,
54
+ manifest,
44
55
  priority = false,
45
56
  sizes,
46
57
  class: className,
47
58
  loading,
48
59
  } = Astro.props;
49
60
 
50
- // Render decision (placeholder / direct rehost / Shopify ?width=) lives in
61
+ // Render decision (placeholder / captured variants / direct rehost / Shopify ?width=) lives in
51
62
  // lib/assets/shared/imageAttrs.ts so it's unit-testable without rendering this component.
52
- const { src: resolvedSrc, srcset } = resolveImage({ src, rehosted, width, height, seed, label });
63
+ const { src: resolvedSrc, srcset } = resolveImage({
64
+ src,
65
+ rehosted,
66
+ width,
67
+ height,
68
+ seed,
69
+ label,
70
+ variants,
71
+ manifest,
72
+ });
73
+ const resolvedSizes =
74
+ srcset || isCapturedPoolRaster(resolvedSrc)
75
+ ? (sizes ?? "(min-width: 768px) 50vw, 100vw")
76
+ : undefined;
53
77
 
54
78
  const loadingAttr = loading ?? (priority ? "eager" : "lazy");
55
79
  ---
@@ -57,7 +81,7 @@ const loadingAttr = loading ?? (priority ? "eager" : "lazy");
57
81
  <img
58
82
  src={resolvedSrc}
59
83
  srcset={srcset}
60
- sizes={srcset ? (sizes ?? "(min-width: 768px) 50vw, 100vw") : undefined}
84
+ sizes={resolvedSizes}
61
85
  alt={alt}
62
86
  width={width}
63
87
  height={height}
@@ -10,6 +10,7 @@ import type { CatalogProduct } from "@tot/public-runtime";
10
10
  import { discountPercent, truncate } from "@/lib/content/shared/format";
11
11
  import { isOnSale, isNew, isAvailable, fromPrice } from "@/lib/catalog/tot/query";
12
12
  import { colorOptionValues, resolveSwatch, isLightFill } from "@/lib/content/shared/colors";
13
+ import type { ComponentProps } from "astro/types";
13
14
  import Img from "@/components/assets/storefront/Img.astro";
14
15
  import Badge from "@/components/content/storefront/Badge.astro";
15
16
  import PriceDisplay from "@/components/catalog/storefront/commerce/PriceDisplay.astro";
@@ -28,9 +29,11 @@ interface Props {
28
29
  rating?: number;
29
30
  /** Optional review count shown with the stars. */
30
31
  reviewCount?: number;
32
+ /** Optional captured width-variant manifest for product card images. */
33
+ variantManifest?: ComponentProps<typeof Img>["manifest"];
31
34
  }
32
35
 
33
- const { product, priority = false, rating, reviewCount } = Astro.props;
36
+ const { product, priority = false, rating, reviewCount, variantManifest = null } = Astro.props;
34
37
  const { tenant, basePath, features, commerce, apiBasePath, browserEnvironment, contentVersionId } = Astro.locals;
35
38
  const showBadges = features.productBadges;
36
39
 
@@ -124,6 +127,7 @@ const quickview = {
124
127
  <Img
125
128
  src={primary.url}
126
129
  rehosted={primary.rehosted}
130
+ manifest={variantManifest}
127
131
  seed={primary.id ?? product.handle}
128
132
  label={product.title}
129
133
  alt={primary.alt ?? product.title}
@@ -151,6 +155,7 @@ const quickview = {
151
155
  <Img
152
156
  src={secondary.url}
153
157
  rehosted={secondary.rehosted}
158
+ manifest={variantManifest}
154
159
  seed={secondary.id ?? `${product.handle}-2`}
155
160
  label={product.title}
156
161
  alt=""
@@ -16,13 +16,22 @@
16
16
  import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
17
17
  import type { CatalogImage } from "@tot/public-runtime";
18
18
  import { placeholderDataUri, isUsableImageUrl } from "@/lib/content/shared/placeholder";
19
- import { resolveImage } from "@/lib/assets/shared/imageAttrs";
19
+ import {
20
+ isCapturedPoolRaster,
21
+ resolveImage,
22
+ type CapturedImageVariant,
23
+ type CapturedVariantManifest,
24
+ } from "@/lib/assets/shared/imageAttrs";
20
25
 
21
26
  export type PublicGalleryImage = Pick<CatalogImage,
22
- "id" | "url" | "alt" | "position" | "width" | "height" | "variant_ids" | "rehosted">;
27
+ "id" | "url" | "alt" | "position" | "width" | "height" | "variant_ids" | "rehosted"> & {
28
+ variants?: readonly CapturedImageVariant[];
29
+ };
23
30
 
24
31
  /** Project before Astro serializes props; private provenance stays in the catalog. */
25
- export function publicGalleryImages(images: readonly CatalogImage[]): PublicGalleryImage[] {
32
+ export function publicGalleryImages(
33
+ images: readonly (CatalogImage & { variants?: readonly CapturedImageVariant[] })[],
34
+ ): PublicGalleryImage[] {
26
35
  return images.map((image) => ({
27
36
  id: image.id,
28
37
  url: image.url,
@@ -32,6 +41,7 @@ export function publicGalleryImages(images: readonly CatalogImage[]): PublicGall
32
41
  ...(image.height !== undefined ? { height: image.height } : {}),
33
42
  ...(image.variant_ids !== undefined ? { variant_ids: [...image.variant_ids] } : {}),
34
43
  ...(image.rehosted !== undefined ? { rehosted: image.rehosted } : {}),
44
+ ...(image.variants !== undefined ? { variants: [...image.variants] } : {}),
35
45
  }));
36
46
  }
37
47
 
@@ -40,6 +50,7 @@ interface Props {
40
50
  title: string;
41
51
  productHandle: string;
42
52
  activeImageId?: string;
53
+ manifest?: CapturedVariantManifest | null;
43
54
  }
44
55
 
45
56
  const FALLBACK_W = 1000;
@@ -53,7 +64,11 @@ const isShopifyCdnUrl = (url: string): boolean =>
53
64
  * decision (same as <Img> on the PLP) so rehosted images use the same
54
65
  * provider-neutral shared-CDN URL on Cloudflare and CloudFront.
55
66
  */
56
- function mainImage(img: PublicGalleryImage, title: string): { src: string; srcset?: string } {
67
+ function mainImage(
68
+ img: PublicGalleryImage,
69
+ title: string,
70
+ manifest?: CapturedVariantManifest | null,
71
+ ): { src: string; srcset?: string } {
57
72
  return resolveImage({
58
73
  src: img.url,
59
74
  rehosted: img.rehosted,
@@ -61,6 +76,8 @@ function mainImage(img: PublicGalleryImage, title: string): { src: string; srcse
61
76
  height: img.height ?? FALLBACK_H,
62
77
  seed: img.id || title,
63
78
  label: title,
79
+ variants: img.variants,
80
+ manifest,
64
81
  });
65
82
  }
66
83
 
@@ -99,6 +116,7 @@ export default function ImageGallery({
99
116
  title,
100
117
  productHandle,
101
118
  activeImageId,
119
+ manifest,
102
120
  }: Props) {
103
121
  const ordered = useMemo(
104
122
  () => [...images].sort((a, b) => a.position - b.position),
@@ -192,7 +210,11 @@ export default function ImageGallery({
192
210
  const w = current.width ?? FALLBACK_W;
193
211
  const h = current.height ?? FALLBACK_H;
194
212
  const isLcp = index === 0 && !interacted;
195
- const main = mainImage(current, title);
213
+ const main = mainImage(current, title, manifest);
214
+ const mainSizes =
215
+ main.srcset || isCapturedPoolRaster(main.src)
216
+ ? "(min-width: 768px) 45vw, 100vw"
217
+ : undefined;
196
218
 
197
219
  return (
198
220
  <div class="flex flex-col gap-3 md:flex-row-reverse md:gap-4">
@@ -211,7 +233,7 @@ export default function ImageGallery({
211
233
  <img
212
234
  src={main.src}
213
235
  srcset={main.srcset}
214
- sizes={main.srcset ? "(min-width: 768px) 45vw, 100vw" : undefined}
236
+ sizes={mainSizes}
215
237
  alt={current.alt ?? title}
216
238
  width={w}
217
239
  height={h}
@@ -88,7 +88,7 @@ async function readManifestEntries(
88
88
  const entries: ManifestEntry[] = [];
89
89
  for (const raw of parsed.entries) {
90
90
  if (!raw || typeof raw !== "object") return null;
91
- const entry = raw as Partial<ManifestEntry>;
91
+ const entry = raw as Partial<ManifestEntry> & { variants?: unknown };
92
92
  if (typeof entry.address !== "string" || !ADDRESS_RE.test(entry.address)) return null;
93
93
  if (!Number.isSafeInteger(entry.byteLength) || entry.byteLength! < 1 || entry.byteLength! > maxEntryBytes ||
94
94
  !sealedOriginalEntryAllowed(entry.provenance?.kind, entry.mediaType, entry.sanitized)) return null;
@@ -100,6 +100,21 @@ async function readManifestEntries(
100
100
  provenance: entry.provenance,
101
101
  ...(sanitized ? { sanitized } : {}),
102
102
  });
103
+ if (Array.isArray(entry.variants)) {
104
+ for (const rawVariant of entry.variants) {
105
+ if (!rawVariant || typeof rawVariant !== "object") return null;
106
+ const v = rawVariant as { address?: unknown; byteLength?: unknown; mediaType?: unknown };
107
+ if (typeof v.address !== "string" || !ADDRESS_RE.test(v.address)) return null;
108
+ if (!Number.isSafeInteger(v.byteLength) || (v.byteLength as number) < 1 || (v.byteLength as number) > maxEntryBytes) return null;
109
+ if (v.mediaType !== "image/webp") return null;
110
+ entries.push({
111
+ address: v.address,
112
+ byteLength: v.byteLength as number,
113
+ mediaType: "image/webp",
114
+ provenance: entry.provenance,
115
+ });
116
+ }
117
+ }
103
118
  }
104
119
  return entries;
105
120
  } catch {
@@ -4,6 +4,7 @@
4
4
  * Picks the `src` + responsive `srcset` for a catalog image, factored out of
5
5
  * Img.astro so the branching is unit-testable without rendering the component:
6
6
  * - unusable/missing URL -> deterministic placeholder data URI
7
+ * - captured/rehosted asset with manifest variants -> shared CDN URL + variant `srcset`
7
8
  * - rehosted asset -> shared CDN URL as-is (Cloudflare + CloudFront safe)
8
9
  * - referenced Shopify CDN -> Shopify's native ?width= scheme
9
10
  * - anything else usable -> the URL as-is, no srcset
@@ -11,6 +12,93 @@
11
12
  import { placeholderDataUri, isUsableImageUrl } from "../../content/shared/placeholder.js";
12
13
 
13
14
  const RESPONSIVE_IMAGE_WIDTHS = [320, 480, 640, 960, 1280, 1600] as const;
15
+ const CAPTURED_POOL_RASTER_RE = /^((?:\/[A-Za-z0-9._~%-]+)*)\/_a\/([0-9a-f]{64})(\.(?:jpe?g|png|webp))?$/i;
16
+
17
+ export interface CapturedImageVariant {
18
+ width: number;
19
+ /** Content address (`<64-hex>`) in the shared `/_a/` asset pool. */
20
+ address?: string;
21
+ /** Pool entry filename (`<64-hex>.webp`). */
22
+ entry?: string;
23
+ /** Explicit URL override when already formatted. */
24
+ url?: string;
25
+ mediaType?: string;
26
+ }
27
+
28
+ export type CapturedVariantManifest =
29
+ | Readonly<Record<string, readonly CapturedImageVariant[]>>
30
+ | ReadonlyMap<string, readonly CapturedImageVariant[]>;
31
+
32
+ let activeCapturedVariantManifest: CapturedVariantManifest | null = null;
33
+
34
+ /** Bind a request/render-scoped captured variant manifest for `<Img>` and `<ImageGallery>`. */
35
+ export function setCapturedVariantManifest(manifest: CapturedVariantManifest | null): void {
36
+ activeCapturedVariantManifest = manifest;
37
+ }
38
+
39
+ export function getCapturedVariantManifest(): CapturedVariantManifest | null {
40
+ return activeCapturedVariantManifest;
41
+ }
42
+
43
+ export function isCapturedPoolRaster(url: string | undefined): boolean {
44
+ if (!url) return false;
45
+ const match = CAPTURED_POOL_RASTER_RE.exec(url);
46
+ if (!match) return false;
47
+ const ext = (match[3] ?? "").toLowerCase();
48
+ return ext === "" || ext === ".jpg" || ext === ".jpeg" || ext === ".png" || ext === ".webp";
49
+ }
50
+
51
+ function lookupManifestVariants(
52
+ manifest: CapturedVariantManifest | null | undefined,
53
+ url: string,
54
+ ): readonly CapturedImageVariant[] | undefined {
55
+ if (!manifest) return undefined;
56
+ const isMap = (
57
+ value: CapturedVariantManifest,
58
+ ): value is ReadonlyMap<string, readonly CapturedImageVariant[]> =>
59
+ typeof (value as ReadonlyMap<string, readonly CapturedImageVariant[]>).get === "function";
60
+ const get = (key: string): readonly CapturedImageVariant[] | undefined =>
61
+ isMap(manifest) ? manifest.get(key) : manifest[key];
62
+ const direct = get(url);
63
+ if (direct?.length) return direct;
64
+ const match = CAPTURED_POOL_RASTER_RE.exec(url);
65
+ if (!match) return undefined;
66
+ const address = match[2]!.toLowerCase();
67
+ const ext = match[3] ?? "";
68
+ return (
69
+ (ext ? get(`/_a/${address}${ext}`) ?? get(`${address}${ext}`) : undefined) ??
70
+ get(`/_a/${address}`) ??
71
+ get(address)
72
+ );
73
+ }
74
+
75
+ function formatCapturedVariantSrcset(
76
+ url: string,
77
+ variants: readonly CapturedImageVariant[],
78
+ ): string | undefined {
79
+ const prefix = CAPTURED_POOL_RASTER_RE.exec(url)?.[1] ?? "";
80
+ const parts: { width: number; candidate: string }[] = [];
81
+ for (const variant of variants) {
82
+ if (!Number.isSafeInteger(variant.width) || variant.width < 1) continue;
83
+ let candidateUrl = variant.url;
84
+ if (!candidateUrl) {
85
+ if (variant.entry) {
86
+ candidateUrl = `${prefix}/_a/${variant.entry}`;
87
+ } else if (variant.address) {
88
+ const ext = variant.mediaType === "image/png"
89
+ ? ".png"
90
+ : variant.mediaType === "image/jpeg"
91
+ ? ".jpg"
92
+ : ".webp";
93
+ candidateUrl = `${prefix}/_a/${variant.address}${ext}`;
94
+ }
95
+ }
96
+ if (candidateUrl) parts.push({ width: variant.width, candidate: `${candidateUrl} ${variant.width}w` });
97
+ }
98
+ if (parts.length === 0) return undefined;
99
+ parts.sort((a, b) => a.width - b.width);
100
+ return parts.map((p) => p.candidate).join(", ");
101
+ }
14
102
 
15
103
  export interface ResolveImageInput {
16
104
  src?: string;
@@ -20,6 +108,10 @@ export interface ResolveImageInput {
20
108
  /** Placeholder seed (image id / product handle). */
21
109
  seed: string;
22
110
  label?: string;
111
+ /** Captured width variants from the revision manifest, when present. */
112
+ variants?: readonly CapturedImageVariant[];
113
+ /** Optional manifest mapping source URLs or `/_a/<address>` entries to width variants. */
114
+ manifest?: CapturedVariantManifest | null;
23
115
  }
24
116
 
25
117
  export interface ResolvedImage {
@@ -31,13 +123,21 @@ const isShopifyCdnUrl = (url: string): boolean =>
31
123
  /\/\/cdn\.shopify\.com\//.test(url) || /\/cdn\/shop\//.test(url);
32
124
 
33
125
  export function resolveImage(input: ResolveImageInput): ResolvedImage {
34
- const { src, rehosted = false, width, height, seed, label } = input;
126
+ const { src, rehosted = false, width, height, seed, label, variants, manifest } = input;
35
127
 
36
128
  if (!isUsableImageUrl(src, rehosted)) {
37
129
  return { src: placeholderDataUri({ seed, width, height, label }) };
38
130
  }
39
131
  const url = src!;
40
132
 
133
+ const resolvedVariants = variants?.length
134
+ ? variants
135
+ : lookupManifestVariants(manifest ?? activeCapturedVariantManifest, url);
136
+ if (resolvedVariants?.length) {
137
+ const srcset = formatCapturedVariantSrcset(url, resolvedVariants);
138
+ if (srcset) return { src: url, srcset };
139
+ }
140
+
41
141
  // Rehosted assets live behind the shared asset CDN. Serve that URL directly:
42
142
  // `/cdn-cgi/image` exists only on zones configured for Cloudflare Image
43
143
  // Resizing and is not a CloudFront contract. The original URL is the one
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "6.11.6",
3
+ "version": "6.11.7",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",