@doswiftly/storefront-sdk 22.6.0 → 22.7.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 +6 -0
- package/README.md +2 -1
- package/dist/core/image.d.ts.map +1 -1
- package/dist/core/image.js +40 -10
- package/dist/next/image-loader.d.ts.map +1 -1
- package/dist/next/image-loader.js +26 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 22.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 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).
|
|
8
|
+
|
|
3
9
|
## 22.6.0
|
|
4
10
|
|
|
5
11
|
### 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
|
-
|
|
|
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
|
package/dist/core/image.d.ts.map
CHANGED
|
@@ -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,MAAM,GAAG,IAAI,CAAC;CAC3B;AAMD;;;GAGG;AACH,eAAO,MAAM,kBAAkB,6BAA6B,CAAC;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;
|
|
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,MAAM,GAAG,IAAI,CAAC;CAC3B;AAMD;;;GAGG;AACH,eAAO,MAAM,kBAAkB,6BAA6B,CAAC;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"}
|
package/dist/core/image.js
CHANGED
|
@@ -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-media/{suffix}`. The content hash in the filename is the
|
|
63
|
+
* version, so the CDN key is immutable (no `?v=` cache-bust).
|
|
64
|
+
*
|
|
65
|
+
* @sync-with backend deploy mirror (`_next/static/media/` prefix → `_next-media/` 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
|
|
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
|
|
76
|
-
if (!config.shopId
|
|
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=`
|
|
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
|
|
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-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-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))
|
|
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
|
-
//
|
|
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.
|
|
@@ -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,
|
|
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
|
-
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doswiftly/storefront-sdk",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.7.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,
|