@doswiftly/storefront-sdk 22.6.0 → 22.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # Changelog
2
2
 
3
+ ## 22.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9039ba1: Add `getAssetPrefix()` for serving your build's static assets from a CDN domain.
8
+
9
+ **Why**: A build's JavaScript, CSS, and font files (`/_next/static/*`) are content-hashed and immutable. Serving them from a CDN domain — rather than your app origin — makes them cache-friendly and keeps them off your storefront server's request path.
10
+
11
+ **Additive (backward-compatible)**: a new export from `@doswiftly/storefront-sdk/next`. Nothing changes until you opt in.
12
+
13
+ **Usage** — add two lines to `next.config`:
14
+
15
+ ```ts
16
+ // next.config.ts
17
+ import { getAssetPrefix } from "@doswiftly/storefront-sdk/next";
18
+
19
+ const nextConfig = {
20
+ assetPrefix: getAssetPrefix(),
21
+ crossOrigin: "anonymous",
22
+ // ...your existing config
23
+ };
24
+ export default nextConfig;
25
+ ```
26
+
27
+ `getAssetPrefix()` returns the CDN base injected by the deploy pipeline (namespaced to your shop), or `undefined` in development and on deploys without a CDN configured — so `next dev` and un-provisioned deploys keep serving assets from the app origin. Pair it with `crossOrigin: 'anonymous'`: the assets then load cross-origin and the CDN sends the matching CORS header (required for fonts).
28
+
29
+ **Migration checklist for existing storefronts**:
30
+ - [ ] Add `assetPrefix: getAssetPrefix()` and `crossOrigin: 'anonymous'` to `next.config`.
31
+ - [ ] Redeploy — the next build's static assets are served from the CDN.
32
+
33
+ (`@doswiftly/storefront-operations` is version-synced with the SDK; no operation changes in this release.)
34
+
35
+ ## 22.7.0
36
+
37
+ ### Minor Changes
38
+
39
+ - 1bbf3e0: `<Image>` now optimizes images imported in code (`import hero from './hero.webp'`), not just `public/` ones. Such imports are bundled under `/_next/static/media/` and were previously served at full size — one large file for every `srcset` width. The loader now routes them through the image CDN, so they are resized per breakpoint and format-negotiated, typically a 90%+ byte reduction on large gallery/hero images. No code change is required: existing `import` + `<Image>` usage is optimized automatically on the next deploy. A dev-only console hint additionally flags `<Image>` sources that still bypass the CDN (for example external URLs).
40
+
3
41
  ## 22.6.0
4
42
 
5
43
  ### Minor Changes
package/README.md CHANGED
@@ -774,7 +774,8 @@ The loader is global and handles each `<Image src>` by category:
774
774
  |-------|----------|
775
775
  | Product image (CDN URL from the GraphQL API) | Width set per `srcset` entry — drop `transform: { maxWidth }`, the loader owns the width |
776
776
  | Local `public/` image (e.g. `/hero.webp`) | Routed through the CDN, resized + format-negotiated |
777
- | Build asset (`/_next/*`), absolute external URL, `data:` URI | Returned unchanged |
777
+ | Image imported in code (`import hero from './hero.webp'`) | Routed through the CDN — the framework bundles it under `/_next/static/media/`, the loader resizes it per `srcset` (no code change needed) |
778
+ | Other build asset (`/_next/*` JS/CSS, `/_nuxt/`, `/_astro/`, `/_app/`), absolute external URL, `data:` URI, SVG | Returned unchanged |
778
779
 
779
780
  The loader reads `NEXT_PUBLIC_SHOP_ID`, `NEXT_PUBLIC_DEPLOYMENT_COMMIT`, and
780
781
  `NEXT_PUBLIC_IMGPROXY_BASE` — all injected by the platform at build/deploy time. When
@@ -1 +1 @@
1
- {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/core/image.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,gFAAgF;IAChF,GAAG,EAAE,MAAM,CAAC;IACZ,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,qCAAqC;IACrC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,sCAAsC;IACtC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,eAAe;IACf,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,2GAA2G;IAC3G,SAAS,CAAC,EAAE,MAA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;AAE7D,6EAA6E;AAC7E,MAAM,WAAW,iBAAiB;IAChC,kFAAkF;IAClF,MAAM,EAAE,MAAM,CAAC;IACf,2GAA2G;IAC3G,OAAO,EAAE,MAAM,CAAC;IAChB,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACxC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,uDAAwD,CAAC;AA2C9F,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,iBAAiB,EACzB,IAAI,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnC,MAAM,CAwDR;AAOD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAiD5F"}
@@ -55,6 +55,20 @@ export const FRAMEWORK_BUILD_PREFIXES = ['/_next/', '/_nuxt/', '/_astro/', '/_ap
55
55
  * @sync-with backend cli.controller.ts `isPublicImageKey`
56
56
  */
57
57
  const LOADER_IMAGE_EXTENSIONS = /\.(png|jpe?g|webp|avif|gif)$/i;
58
+ /**
59
+ * Path prefix of Next.js build-output **media** (images imported in code, e.g.
60
+ * `import hero from './hero.webp'` → content-hashed bundle). UNLIKE the rest of `/_next/*`
61
+ * (JS/CSS chunks, left untouched), media images ARE routed through the resize CDN: the deploy
62
+ * mirrors them to `s/{shopId}/_next/static/media/{suffix}` (the path is kept 1:1). The content
63
+ * hash in the filename is the version, so the CDN key is immutable (no `?v=` cache-bust).
64
+ *
65
+ * @sync-with backend deploy mirror (`_next/static/media/` path mirrored 1:1 into the R2 key)
66
+ */
67
+ const NEXT_STATIC_MEDIA_PREFIX = '/_next/static/media/';
68
+ /** Suffix after {@link NEXT_STATIC_MEDIA_PREFIX}, or `null` if `pathname` is not a Next media path. */
69
+ function nextStaticMediaSuffix(pathname) {
70
+ return pathname.startsWith(NEXT_STATIC_MEDIA_PREFIX) ? pathname.slice(NEXT_STATIC_MEDIA_PREFIX.length) : null;
71
+ }
58
72
  export function buildImageLoaderUrl(config, args) {
59
73
  // `||` (not `??`): an empty injected base must still fall back to the platform default.
60
74
  const base = (config.cdnBase || IMAGE_CDN_BASE_URL).replace(/\/+$/, '');
@@ -70,26 +84,42 @@ export function buildImageLoaderUrl(config, args) {
70
84
  return src;
71
85
  }
72
86
  }
73
- // (2) Local public/ image — root-relative, NOT protocol-relative (`//host`).
87
+ // (2) Local root-relative image — NOT protocol-relative (`//host`). Two sub-cases:
88
+ // (2a) a Next build-output media image (`/_next/static/media/*`), and (2b) a `public/` image.
74
89
  if (src.startsWith('/') && !src.startsWith('//')) {
75
- // Not in a deployed storefront (no shop/version) → leave untouched, no resize.
76
- if (!config.shopId || !config.version)
90
+ // Not in a deployed storefront (no shopId) → leave untouched, no resize.
91
+ if (!config.shopId)
77
92
  return src;
78
- // Drop any author query/hash — the loader owns `width` and the `?v=` cache-bust.
93
+ // Drop any author query/hash — the loader owns `width` (and, for public/, the `?v=`).
79
94
  const pathname = src.replace(/[?#].*$/, '');
80
- // Only mirrored image formats; never a framework build asset (any supported framework).
95
+ // Only mirrored image formats. SVG + every non-image (JS/CSS/fonts…) pass through — the
96
+ // extension gate is the safety that keeps framework build chunks out of the resize CDN.
97
+ if (!LOADER_IMAGE_EXTENSIONS.test(pathname))
98
+ return src;
99
+ // (2a) Next build-output media image. The content hash in the filename IS the version, so
100
+ // the CDN key is immutable → no `?v=`. The deploy mirrors these to `s/{shopId}/_next/static/media/`.
101
+ const mediaSuffix = nextStaticMediaSuffix(pathname);
102
+ if (mediaSuffix !== null) {
103
+ // `encodeURIComponent` keeps `..` (unreserved) — harmless while the image CDN is unsigned +
104
+ // public; if HMAC signing is ever added, strip `..` segments here AND in the public/ branch.
105
+ const encoded = mediaSuffix.split('/').map(encodeURIComponent).join('/');
106
+ return `${base}/s/${config.shopId}/_next/static/media/${encoded}?width=${width}`;
107
+ }
108
+ // Any other framework build output (hashed/immutable, NOT mirrored) → untouched.
81
109
  const buildPrefixes = config.buildAssetPrefixes ?? FRAMEWORK_BUILD_PREFIXES;
82
- if (buildPrefixes.some((p) => pathname.startsWith(p)) || !LOADER_IMAGE_EXTENSIONS.test(pathname))
110
+ if (buildPrefixes.some((p) => pathname.startsWith(p)))
111
+ return src;
112
+ // (2b) Local public/ image. Name-stable key → needs the deploy version for the `?v=` cache-
113
+ // bust (the source key is overwritten in place when a public/ image is replaced).
114
+ if (!config.version)
83
115
  return src;
84
- // Encode per segment so spaces / non-ASCII filenames match the raw R2 key after the
85
- // CDN percent-decodes the path (keeps `/` as the separator).
116
+ // Encode per segment so spaces / non-ASCII filenames match the raw R2 key after the CDN
117
+ // percent-decodes the path (keeps `/` as the separator).
86
118
  const path = pathname
87
119
  .replace(/^\/+/, '')
88
120
  .split('/')
89
121
  .map(encodeURIComponent)
90
122
  .join('/');
91
- // Name-stable CDN key; the deploy version rides in `?v=` to bust the CDN cache when a
92
- // public/ image is replaced under the same name (the source key is overwritten in place).
93
123
  return `${base}/s/${config.shopId}/public/${path}?width=${width}&v=${config.version}`;
94
124
  }
95
125
  // (3) Build assets, protocol-relative/external URLs, data URIs — untouched.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Next.js `assetPrefix` for DoSwiftly storefronts.
3
+ *
4
+ * Wire it once:
5
+ * ```ts
6
+ * // next.config.ts
7
+ * import { getAssetPrefix } from '@doswiftly/storefront-sdk/next';
8
+ * const nextConfig = { assetPrefix: getAssetPrefix(), crossOrigin: 'anonymous' };
9
+ * ```
10
+ *
11
+ * Your static assets (`/_next/static/*` — JavaScript, CSS, and fonts) are then served from your
12
+ * storefront's own CDN domain instead of the app origin: cache-friendly, immutable, and off the
13
+ * request path of your storefront server. The CDN base is injected by the DoSwiftly deploy
14
+ * pipeline — you don't configure it.
15
+ *
16
+ * Returns `undefined` in development (so `doswiftly dev` serves assets locally for fast refresh) and
17
+ * whenever the pipeline hasn't injected a CDN base (so an un-provisioned deploy keeps serving
18
+ * assets from the app origin). Either way there is no setup and nothing to break.
19
+ *
20
+ * Pair it with `crossOrigin: 'anonymous'`: assets then load cross-origin and the CDN sends the
21
+ * matching CORS header (required for fonts).
22
+ */
23
+ export declare function getAssetPrefix(): string | undefined;
24
+ //# sourceMappingURL=asset-prefix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asset-prefix.d.ts","sourceRoot":"","sources":["../../src/next/asset-prefix.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,IAAI,MAAM,GAAG,SAAS,CAKnD"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Next.js `assetPrefix` for DoSwiftly storefronts.
3
+ *
4
+ * Wire it once:
5
+ * ```ts
6
+ * // next.config.ts
7
+ * import { getAssetPrefix } from '@doswiftly/storefront-sdk/next';
8
+ * const nextConfig = { assetPrefix: getAssetPrefix(), crossOrigin: 'anonymous' };
9
+ * ```
10
+ *
11
+ * Your static assets (`/_next/static/*` — JavaScript, CSS, and fonts) are then served from your
12
+ * storefront's own CDN domain instead of the app origin: cache-friendly, immutable, and off the
13
+ * request path of your storefront server. The CDN base is injected by the DoSwiftly deploy
14
+ * pipeline — you don't configure it.
15
+ *
16
+ * Returns `undefined` in development (so `doswiftly dev` serves assets locally for fast refresh) and
17
+ * whenever the pipeline hasn't injected a CDN base (so an un-provisioned deploy keeps serving
18
+ * assets from the app origin). Either way there is no setup and nothing to break.
19
+ *
20
+ * Pair it with `crossOrigin: 'anonymous'`: assets then load cross-origin and the CDN sends the
21
+ * matching CORS header (required for fonts).
22
+ */
23
+ export function getAssetPrefix() {
24
+ // Development must serve assets from the dev server (fast refresh) — never the CDN.
25
+ if (process.env.NODE_ENV === "development")
26
+ return undefined;
27
+ // Literal member access (read at build time inside next.config). Empty / unset → app origin.
28
+ return process.env.DOSWIFTLY_ASSET_PREFIX || undefined;
29
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"image-loader.d.ts","sourceRoot":"","sources":["../../src/next/image-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAuB,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE5E,yDAAyD;AACzD,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC,GAClC,CAAC,IAAI,EAAE,mBAAmB,KAAK,MAAM,CAOvC"}
1
+ {"version":3,"file":"image-loader.d.ts","sourceRoot":"","sources":["../../src/next/image-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAuB,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE5E,yDAAyD;AACzD,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC,GAClC,CAAC,IAAI,EAAE,mBAAmB,KAAK,MAAM,CAevC"}
@@ -40,5 +40,30 @@ export function createImageLoader(config) {
40
40
  version: config?.version ?? process.env.NEXT_PUBLIC_DEPLOYMENT_COMMIT ?? '',
41
41
  cdnBase: config?.cdnBase ?? process.env.NEXT_PUBLIC_IMGPROXY_BASE,
42
42
  };
43
- return (args) => buildImageLoaderUrl(resolved, args);
43
+ // Dev-only hint: surface raster images that the loader can't optimize (an external host, or
44
+ // a build asset outside `/_next/static/media/`). Only when a shopId is resolved — without one
45
+ // (local `doswiftly dev`) every image is an expected passthrough, so warning would be noise.
46
+ const warned = resolved.shopId && process.env.NODE_ENV !== 'production' ? new Set() : null;
47
+ return (args) => {
48
+ const url = buildImageLoaderUrl(resolved, args);
49
+ if (warned && url === args.src)
50
+ warnImageBypass(args.src, warned);
51
+ return url;
52
+ };
53
+ }
54
+ /**
55
+ * Dev-only: warn once per src for an EXTERNAL raster image used in `<Image>` — it bypasses the
56
+ * DoSwiftly image CDN (served as-is, no resize / format negotiation). Local `/…` images (`public/`
57
+ * + code-imported `/_next/static/media/`) are routed automatically; a local image reaching here
58
+ * only means a missing platform env (a config issue, not a footgun), so it is NOT flagged. SVG
59
+ * (vector) and `data:` URIs are not bypasses.
60
+ */
61
+ function warnImageBypass(src, warned) {
62
+ if (warned.has(src) || !/^(https?:)?\/\//.test(src) || !/\.(png|jpe?g|webp|avif|gif)(\?|#|$)/i.test(src))
63
+ return;
64
+ warned.add(src);
65
+ console.warn(`[DoSwiftly] <Image src="${src}"> is an external URL, so it loads at full size and is NOT ` +
66
+ `optimized (no automatic resizing or format conversion). To optimize it: move the image into ` +
67
+ `your public/ folder and use a root-relative src like "/photo.jpg", or use a product image ` +
68
+ `whose URL comes from the API (those are already optimized CDN URLs).`);
44
69
  }
@@ -6,4 +6,5 @@
6
6
  * core stays portable to Node, Edge, Deno, and Bun.
7
7
  */
8
8
  export { createImageLoader, type NextImageLoaderArgs } from './image-loader';
9
+ export { getAssetPrefix } from './asset-prefix';
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/next/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC"}
@@ -6,6 +6,7 @@
6
6
  * core stays portable to Node, Edge, Deno, and Bun.
7
7
  */
8
8
  export { createImageLoader } from './image-loader';
9
+ export { getAssetPrefix } from './asset-prefix';
9
10
  // The pure builder + its config/base (`buildImageLoaderUrl`, `IMAGE_CDN_BASE_URL`,
10
11
  // `ImageLoaderConfig`) are framework-agnostic and live on the core entry
11
12
  // (`@doswiftly/storefront-sdk`) — import them from there, not from `/next`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doswiftly/storefront-sdk",
3
- "version": "22.6.0",
3
+ "version": "22.8.0",
4
4
  "description": "Storefront runtime SDK for DoSwiftly Commerce — layered transport, middleware pipeline, React providers, Zustand stores, cache strategies. 0 runtime dependencies in core.",
5
5
  "type": "module",
6
6
  "sideEffects": false,