@decocms/blocks 7.86.6 → 7.87.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.86.6",
3
+ "version": "7.87.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -139,6 +139,40 @@ it("data stores key by dataScope and survive a deploy; build stores do not", asy
139
139
  expect(await run(storage, "site:build-B", () => data2.read(data2.key("item")))).toBeUndefined();
140
140
  });
141
141
 
142
+ it("cachedLoader with dataVersion survives a deploy/publish; without it, it does not", async () => {
143
+ const storage = createKVCacheStorage(fakeKV());
144
+ const bind = <T,>(build: string, fn: () => T): T =>
145
+ RequestContext.run(new Request("https://site.test/"), () => {
146
+ bindCacheStorage({
147
+ storage,
148
+ scope: `site:${build}`,
149
+ dataScope: "site:data:1",
150
+ waitUntil: (work) => pending.push(work),
151
+ });
152
+ return fn();
153
+ });
154
+ const opts = { policy: "stale-while-revalidate", maxAge: 60_000 } as const;
155
+ const fn = vi.fn(async (p: { q: string }) => ({ q: p.q }));
156
+ const data = createCachedLoader("plp", fn, { ...opts, dataVersion: "1" });
157
+ const build = createCachedLoader("plp-build", fn, opts);
158
+ await bind("build-A", async () => {
159
+ await data({ q: "a" });
160
+ await build({ q: "a" });
161
+ await flush();
162
+ });
163
+ expect(fn).toHaveBeenCalledTimes(2);
164
+ clearLoaderCache();
165
+ await bind("build-B", () => data({ q: "a" }));
166
+ expect(fn).toHaveBeenCalledTimes(2); // preserved across the new build
167
+ await bind("build-B", () => build({ q: "a" }));
168
+ expect(fn).toHaveBeenCalledTimes(3); // per-build entry is gone
169
+ // Bumping dataVersion (shape change) or new props miss.
170
+ const bumped = createCachedLoader("plp", fn, { ...opts, dataVersion: "2" });
171
+ await bind("build-B", () => bumped({ q: "a" }));
172
+ await bind("build-B", () => data({ q: "b" }));
173
+ expect(fn).toHaveBeenCalledTimes(5);
174
+ });
175
+
142
176
  it("does not switch bindings between concurrent requests", async () => {
143
177
  const a = createMemoryCacheStorage();
144
178
  const b = createMemoryCacheStorage();
@@ -238,9 +238,10 @@ export function createCacheStore<T>(
238
238
  return entry.value;
239
239
  }
240
240
  return {
241
- key(raw: string): string {
241
+ /** `keyKind` overrides the store's `kind` for this key (same memory tier and budget). */
242
+ key(raw: string, keyKind: "build" | "data" = kind): string {
242
243
  const context = getCacheStorageContext();
243
- const scope = (kind === "data" ? context?.dataScope : undefined) ?? context?.scope;
244
+ const scope = (keyKind === "data" ? context?.dataScope : undefined) ?? context?.scope;
244
245
  return JSON.stringify([scope ?? "local", namespace, raw]);
245
246
  },
246
247
  get,
@@ -64,6 +64,21 @@ export interface CachedLoaderOptions {
64
64
  * Default: every resolved result is stored.
65
65
  */
66
66
  shouldCache?: (result: unknown) => boolean;
67
+ /**
68
+ * Opt this loader's results out of the per-build key. When set, entries are
69
+ * keyed by `dataVersion` instead of the build hash and stored in the request's
70
+ * data scope — which, with `DECO_DATA_CACHE_ON_DEPLOY=preserve`, excludes the
71
+ * build hash and CMS revision, so results survive deploys and publishes
72
+ * (props are still part of the key). Without preserve it behaves as before.
73
+ *
74
+ * Only for loaders whose result is a pure function of props + upstream data:
75
+ * a CMS publish no longer invalidates these entries, so a loader that reads
76
+ * decofile data outside its props would keep the old result until its TTL.
77
+ * Bump the string whenever the result's shape/transform changes: a forgotten
78
+ * bump serves the old shape to the new code until the entry expires. Purge
79
+ * with `DECO_DATA_CACHE_PURGE` or `cache:data-version` in DECO_KV.
80
+ */
81
+ dataVersion?: string;
67
82
  }
68
83
 
69
84
  /**
@@ -280,7 +295,9 @@ export function createCachedLoader<TProps, TResult>(
280
295
  staleWhileRevalidate = 300_000,
281
296
  keyFn = JSON.stringify,
282
297
  shouldCache,
298
+ dataVersion,
283
299
  } = resolved;
300
+ const keyVersion = dataVersion === undefined ? BUILD : `data:${dataVersion}`;
284
301
 
285
302
  const env = typeof globalThis.process !== "undefined" ? globalThis.process.env : undefined;
286
303
  const isDev = env?.DECO_CACHE_DISABLE === "true" || env?.NODE_ENV === "development";
@@ -297,7 +314,10 @@ export function createCachedLoader<TProps, TResult>(
297
314
  if (policy === "no-store") return loaderFn;
298
315
 
299
316
  const cachedFn = async (props: TProps): Promise<TResult> => {
300
- const cacheKey = cache.key(`${BUILD}::${name}::${keyFn(props)}`);
317
+ const cacheKey = cache.key(
318
+ `${keyVersion}::${name}::${keyFn(props)}`,
319
+ dataVersion === undefined ? "build" : "data",
320
+ );
301
321
  // Joining another caller's flight is bounded by a timer of THIS caller's
302
322
  // request (see `joinInflight`): if the owner's request was cancelled and
303
323
  // its Promise will never settle, evict the dead slot and run again.