@decocms/blocks 7.81.0 → 7.81.1
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
package/src/sdk/cachedLoader.ts
CHANGED
|
@@ -69,9 +69,13 @@ export interface CachedLoaderOptions {
|
|
|
69
69
|
* into single-flight dedup (concurrent identical calls in one render collapse to
|
|
70
70
|
* one upstream call — the #339 N+1 fix). `cacheKey` computes the dedup identity
|
|
71
71
|
* from `(props, req)`; returning `null` means "do not dedup, run fresh".
|
|
72
|
+
*
|
|
73
|
+
* `default` receives the Fresh-style `ctx` (AppContext) as its 3rd argument
|
|
74
|
+
* when the caller has one (e.g. `/deco/invoke`). `ctx` never takes part in the
|
|
75
|
+
* dedup key — same as deco, where the cache key is derived from props/req only.
|
|
72
76
|
*/
|
|
73
77
|
export interface LoaderModule<TProps = any, TResult = any> {
|
|
74
|
-
default: (props: TProps, req?: Request) => Promise<TResult>;
|
|
78
|
+
default: (props: TProps, req?: Request, ctx?: any) => Promise<TResult>;
|
|
75
79
|
cache?: CachePolicy | { maxAge: number };
|
|
76
80
|
cacheKey?: (props: TProps, req?: Request) => string | null;
|
|
77
81
|
}
|
|
@@ -488,11 +492,13 @@ const moduleInflight = new Map<string, Promise<unknown>>();
|
|
|
488
492
|
* `RequestContext.current` — both execute inside the same AsyncLocalStorage
|
|
489
493
|
* scope. `cacheKey` returning `null`, or `req` being unavailable when `cacheKey`
|
|
490
494
|
* needs it, falls back to running fresh / keying on `JSON.stringify(props)`.
|
|
495
|
+
* An optional `ctx` is forwarded untouched to `mod.default` and is NOT part of
|
|
496
|
+
* the key: joiners of an in-flight call share the owner's result.
|
|
491
497
|
*/
|
|
492
498
|
export function createCachedLoaderFromModule<TProps, TResult>(
|
|
493
499
|
name: string,
|
|
494
500
|
mod: LoaderModule<TProps, TResult>,
|
|
495
|
-
): (props: TProps, req?: Request) => Promise<TResult> {
|
|
501
|
+
): (props: TProps, req?: Request, ctx?: any) => Promise<TResult> {
|
|
496
502
|
const policy: CachePolicy =
|
|
497
503
|
typeof mod.cache === "string"
|
|
498
504
|
? mod.cache
|
|
@@ -507,7 +513,7 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
507
513
|
|
|
508
514
|
const cacheKeyFn = mod.cacheKey;
|
|
509
515
|
|
|
510
|
-
const dedupedFn = (props: TProps, req?: Request): Promise<TResult> => {
|
|
516
|
+
const dedupedFn = (props: TProps, req?: Request, ctx?: any): Promise<TResult> => {
|
|
511
517
|
const request = req ?? RequestContext.current?.request ?? undefined;
|
|
512
518
|
|
|
513
519
|
let keyPart: string | null;
|
|
@@ -520,7 +526,7 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
520
526
|
}
|
|
521
527
|
|
|
522
528
|
// Explicit null → the loader declared this call uncacheable: run fresh.
|
|
523
|
-
if (keyPart === null) return mod.default(props, req);
|
|
529
|
+
if (keyPart === null) return mod.default(props, req, ctx);
|
|
524
530
|
|
|
525
531
|
const key = cache.key(`${BUILD}::${name}::${keyPart}`);
|
|
526
532
|
const existing = moduleInflight.get(key) as Promise<TResult> | undefined;
|
|
@@ -529,11 +535,11 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
529
535
|
// cancelled never settles — evict it and run again (see `joinInflight`).
|
|
530
536
|
return joinInflight(existing, () => {
|
|
531
537
|
releaseSlot(moduleInflight, key, existing);
|
|
532
|
-
return dedupedFn(props, req);
|
|
538
|
+
return dedupedFn(props, req, ctx);
|
|
533
539
|
});
|
|
534
540
|
}
|
|
535
541
|
|
|
536
|
-
const promise: Promise<TResult> = Promise.resolve(mod.default(props, req)).finally(() => {
|
|
542
|
+
const promise: Promise<TResult> = Promise.resolve(mod.default(props, req, ctx)).finally(() => {
|
|
537
543
|
releaseSlot(moduleInflight, key, promise);
|
|
538
544
|
});
|
|
539
545
|
return publishInflight(moduleInflight, key, promise);
|
|
@@ -551,13 +557,15 @@ export function createCachedLoaderFromModule<TProps, TResult>(
|
|
|
551
557
|
export function createLoaderEntry<TProps = any, TResult = any>(
|
|
552
558
|
name: string,
|
|
553
559
|
importFn: () => Promise<LoaderModule<TProps, TResult>>,
|
|
554
|
-
): (props: TProps, req?: Request) => Promise<TResult> {
|
|
560
|
+
): (props: TProps, req?: Request, ctx?: any) => Promise<TResult> {
|
|
555
561
|
// Memoize the import+build PROMISE (not the resolved wrapper) so concurrent
|
|
556
562
|
// first-calls — exactly the N-sections-per-render case — share one import
|
|
557
563
|
// instead of racing into N. Reset on failure so a transient import error can
|
|
558
564
|
// retry rather than poisoning the entry forever.
|
|
559
|
-
let wrappedPromise:
|
|
560
|
-
|
|
565
|
+
let wrappedPromise:
|
|
566
|
+
| Promise<(props: TProps, req?: Request, ctx?: any) => Promise<TResult>>
|
|
567
|
+
| undefined;
|
|
568
|
+
return (props: TProps, req?: Request, ctx?: any): Promise<TResult> => {
|
|
561
569
|
if (!wrappedPromise) {
|
|
562
570
|
wrappedPromise = importFn()
|
|
563
571
|
.then((mod) => createCachedLoaderFromModule(name, mod))
|
|
@@ -566,7 +574,7 @@ export function createLoaderEntry<TProps = any, TResult = any>(
|
|
|
566
574
|
throw err;
|
|
567
575
|
});
|
|
568
576
|
}
|
|
569
|
-
return wrappedPromise.then((wrapped) => wrapped(props, req));
|
|
577
|
+
return wrappedPromise.then((wrapped) => wrapped(props, req, ctx));
|
|
570
578
|
};
|
|
571
579
|
}
|
|
572
580
|
|
|
@@ -206,4 +206,48 @@ describe("createLoaderEntry — lazy import + wrap", () => {
|
|
|
206
206
|
await entry({ a: 1 });
|
|
207
207
|
expect(fn).toHaveBeenCalledTimes(2);
|
|
208
208
|
});
|
|
209
|
+
it("forwards the Fresh-style ctx (3rd arg) to a no-store loader", async () => {
|
|
210
|
+
const fn = vi.fn(async (_props: unknown, _req?: Request, ctx?: any) => ctx?.cms?.id);
|
|
211
|
+
const entry = createLoaderEntry("site/loaders/ctx-plain", async () => ({ default: fn }));
|
|
212
|
+
const req = new Request("https://store.example/");
|
|
213
|
+
const ctx = { cms: { id: "cms-1" } };
|
|
214
|
+
|
|
215
|
+
await expect(entry({ a: 1 }, req, ctx)).resolves.toBe("cms-1");
|
|
216
|
+
expect(fn).toHaveBeenCalledWith({ a: 1 }, req, ctx);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("forwards ctx to a SWR loader without making it part of the dedup key", async () => {
|
|
220
|
+
let resolve!: () => void;
|
|
221
|
+
const gate = new Promise<void>((r) => (resolve = r));
|
|
222
|
+
const fn = vi.fn(async (_props: unknown, _req?: Request, ctx?: any) => {
|
|
223
|
+
await gate;
|
|
224
|
+
return ctx?.storeConfig?.name;
|
|
225
|
+
});
|
|
226
|
+
const entry = createLoaderEntry("site/loaders/ctx-swr", async () => ({
|
|
227
|
+
default: fn,
|
|
228
|
+
cache: "stale-while-revalidate" as const,
|
|
229
|
+
}));
|
|
230
|
+
const req = new Request("https://store.example/");
|
|
231
|
+
const ctxA = { storeConfig: { name: "a" } };
|
|
232
|
+
const ctxB = { storeConfig: { name: "b" } };
|
|
233
|
+
|
|
234
|
+
// Same props, different ctx objects: still one upstream call (deco parity —
|
|
235
|
+
// ctx never enters the cache key), and the owner's ctx reaches the loader.
|
|
236
|
+
const p1 = entry({ a: 1 }, req, ctxA);
|
|
237
|
+
const p2 = entry({ a: 1 }, req, ctxB);
|
|
238
|
+
resolve();
|
|
239
|
+
await expect(Promise.all([p1, p2])).resolves.toEqual(["a", "a"]);
|
|
240
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
241
|
+
expect(fn).toHaveBeenCalledWith({ a: 1 }, req, ctxA);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("still works for 2-arg callers (ctx undefined)", async () => {
|
|
245
|
+
const fn = vi.fn(async (_props: unknown, _req?: Request, ctx?: any) => ctx);
|
|
246
|
+
const entry = createLoaderEntry("site/loaders/ctx-legacy", async () => ({
|
|
247
|
+
default: fn,
|
|
248
|
+
cache: "stale-while-revalidate" as const,
|
|
249
|
+
}));
|
|
250
|
+
await expect(entry({ a: 1 })).resolves.toBeUndefined();
|
|
251
|
+
expect(fn).toHaveBeenCalledWith({ a: 1 }, undefined, undefined);
|
|
252
|
+
});
|
|
209
253
|
});
|