@doswiftly/storefront-sdk 22.7.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,37 @@
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
+
3
35
  ## 22.7.0
4
36
 
5
37
  ### Minor Changes
@@ -59,10 +59,10 @@ const LOADER_IMAGE_EXTENSIONS = /\.(png|jpe?g|webp|avif|gif)$/i;
59
59
  * Path prefix of Next.js build-output **media** (images imported in code, e.g.
60
60
  * `import hero from './hero.webp'` → content-hashed bundle). UNLIKE the rest of `/_next/*`
61
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).
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
64
  *
65
- * @sync-with backend deploy mirror (`_next/static/media/` prefix `_next-media/` R2 key)
65
+ * @sync-with backend deploy mirror (`_next/static/media/` path mirrored 1:1 into the R2 key)
66
66
  */
67
67
  const NEXT_STATIC_MEDIA_PREFIX = '/_next/static/media/';
68
68
  /** Suffix after {@link NEXT_STATIC_MEDIA_PREFIX}, or `null` if `pathname` is not a Next media path. */
@@ -97,13 +97,13 @@ export function buildImageLoaderUrl(config, args) {
97
97
  if (!LOADER_IMAGE_EXTENSIONS.test(pathname))
98
98
  return src;
99
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/`.
100
+ // the CDN key is immutable → no `?v=`. The deploy mirrors these to `s/{shopId}/_next/static/media/`.
101
101
  const mediaSuffix = nextStaticMediaSuffix(pathname);
102
102
  if (mediaSuffix !== null) {
103
103
  // `encodeURIComponent` keeps `..` (unreserved) — harmless while the image CDN is unsigned +
104
104
  // public; if HMAC signing is ever added, strip `..` segments here AND in the public/ branch.
105
105
  const encoded = mediaSuffix.split('/').map(encodeURIComponent).join('/');
106
- return `${base}/s/${config.shopId}/_next-media/${encoded}?width=${width}`;
106
+ return `${base}/s/${config.shopId}/_next/static/media/${encoded}?width=${width}`;
107
107
  }
108
108
  // Any other framework build output (hashed/immutable, NOT mirrored) → untouched.
109
109
  const buildPrefixes = config.buildAssetPrefixes ?? FRAMEWORK_BUILD_PREFIXES;
@@ -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
+ }
@@ -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.7.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,