@decocms/blocks 7.80.2 → 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 +1 -1
- package/src/cms/client.ts +6 -1
- package/src/cms/deferredTrigger.test.ts +27 -1
- package/src/cms/deferredTrigger.ts +19 -0
- package/src/cms/index.ts +6 -1
- package/src/cms/resolve.test.ts +9 -0
- package/src/cms/resolve.ts +15 -1
- package/src/sdk/cachedLoader.ts +18 -10
- package/src/sdk/cachedLoaderModule.test.ts +44 -0
package/package.json
CHANGED
package/src/cms/client.ts
CHANGED
|
@@ -37,7 +37,12 @@
|
|
|
37
37
|
* `@decocms/blocks/cms` instead.
|
|
38
38
|
*/
|
|
39
39
|
export type { DeferredTrigger } from "./deferredTrigger";
|
|
40
|
-
export {
|
|
40
|
+
export {
|
|
41
|
+
DEFAULT_DEFERRED_ROOT_MARGIN,
|
|
42
|
+
DEFAULT_DEFERRED_TRIGGER,
|
|
43
|
+
getDeferredRootMargin,
|
|
44
|
+
getDeferredTrigger,
|
|
45
|
+
} from "./deferredTrigger";
|
|
41
46
|
export type { OnBeforeResolveProps, SectionModule, SectionOptions } from "./registry";
|
|
42
47
|
export {
|
|
43
48
|
getResolvedComponent,
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_DEFERRED_ROOT_MARGIN,
|
|
5
|
+
DEFAULT_DEFERRED_TRIGGER,
|
|
6
|
+
getDeferredRootMargin,
|
|
7
|
+
getDeferredTrigger,
|
|
8
|
+
} from "./deferredTrigger";
|
|
4
9
|
|
|
5
10
|
/**
|
|
6
11
|
* `getDeferredTrigger()` is the client-side half of `deferredTrigger`. It must
|
|
@@ -30,3 +35,24 @@ describe("getDeferredTrigger", () => {
|
|
|
30
35
|
expect(getDeferredTrigger()).toBe("load");
|
|
31
36
|
});
|
|
32
37
|
});
|
|
38
|
+
|
|
39
|
+
describe("getDeferredRootMargin", () => {
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
delete (globalThis as any).__deco;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("keeps 300px when setAsyncRenderingConfig() never ran", () => {
|
|
45
|
+
expect(getDeferredRootMargin()).toBe("300px");
|
|
46
|
+
expect(DEFAULT_DEFERRED_ROOT_MARGIN).toBe("300px");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("keeps 300px when the config exists but omits the option", () => {
|
|
50
|
+
(globalThis as any).__deco = { asyncConfig: { deferredTrigger: "intersection" } };
|
|
51
|
+
expect(getDeferredRootMargin()).toBe("300px");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("reads a configured margin off the shared global", () => {
|
|
55
|
+
(globalThis as any).__deco = { asyncConfig: { deferredRootMargin: "200px" } };
|
|
56
|
+
expect(getDeferredRootMargin()).toBe("200px");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -56,3 +56,22 @@ export function getDeferredTrigger(): DeferredTrigger {
|
|
|
56
56
|
const config = (globalThis as any).__deco?.asyncConfig;
|
|
57
57
|
return config?.deferredTrigger ?? DEFAULT_DEFERRED_TRIGGER;
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* How far outside the viewport a deferred (⚡) section's skeleton may be when
|
|
62
|
+
* `"intersection"` fetches it — the observer's `rootMargin`.
|
|
63
|
+
*
|
|
64
|
+
* Deco on Fresh's `DispatchAsyncRender` observes with `"200px"`. The default
|
|
65
|
+
* here stays `"300px"` so a framework bump changes nothing; a migrated site
|
|
66
|
+
* that needs Fresh's request timing sets
|
|
67
|
+
* `setAsyncRenderingConfig({ deferredRootMargin: "200px" })`. The 100px matter
|
|
68
|
+
* on a phone: a shelf placed just under a tall product page lands inside a
|
|
69
|
+
* 300px margin on load, and its request goes out before the shopper scrolls.
|
|
70
|
+
*/
|
|
71
|
+
export const DEFAULT_DEFERRED_ROOT_MARGIN = "300px";
|
|
72
|
+
|
|
73
|
+
/** Read the configured `rootMargin`, falling back to `"300px"`. */
|
|
74
|
+
export function getDeferredRootMargin(): string {
|
|
75
|
+
const config = (globalThis as any).__deco?.asyncConfig;
|
|
76
|
+
return config?.deferredRootMargin ?? DEFAULT_DEFERRED_ROOT_MARGIN;
|
|
77
|
+
}
|
package/src/cms/index.ts
CHANGED
|
@@ -18,7 +18,12 @@ export {
|
|
|
18
18
|
snapshotKey,
|
|
19
19
|
} from "./blockSource";
|
|
20
20
|
export type { DeferredTrigger } from "./deferredTrigger";
|
|
21
|
-
export {
|
|
21
|
+
export {
|
|
22
|
+
DEFAULT_DEFERRED_ROOT_MARGIN,
|
|
23
|
+
DEFAULT_DEFERRED_TRIGGER,
|
|
24
|
+
getDeferredRootMargin,
|
|
25
|
+
getDeferredTrigger,
|
|
26
|
+
} from "./deferredTrigger";
|
|
22
27
|
export type {
|
|
23
28
|
DraftPointer,
|
|
24
29
|
ResolveDraftForRequestOptions,
|
package/src/cms/resolve.test.ts
CHANGED
|
@@ -500,6 +500,15 @@ describe("async rendering config defaults", () => {
|
|
|
500
500
|
expect(cfg!.respectCmsLazy).toBe(true);
|
|
501
501
|
expect(cfg!.botAwareSeo).toBe(false); // opt-in — off by default
|
|
502
502
|
expect(cfg!.deferredTrigger).toBe("intersection"); // opt-in — a bump changes nothing
|
|
503
|
+
expect(cfg!.deferredRootMargin).toBe("300px"); // same margin as before the option existed
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("carries deferredRootMargin through a later partial call", () => {
|
|
507
|
+
setAsyncRenderingConfig({ deferredRootMargin: "200px" });
|
|
508
|
+
expect(getAsyncRenderingConfig()!.deferredRootMargin).toBe("200px");
|
|
509
|
+
|
|
510
|
+
setAsyncRenderingConfig({ alwaysEager: ["site/sections/Header.tsx"] });
|
|
511
|
+
expect(getAsyncRenderingConfig()!.deferredRootMargin).toBe("200px");
|
|
503
512
|
});
|
|
504
513
|
|
|
505
514
|
it("carries deferredTrigger through, and a later partial call does not reset it", () => {
|
package/src/cms/resolve.ts
CHANGED
|
@@ -8,7 +8,11 @@ import { parseSegmentCookie, SEGMENT_COOKIE, type StoredFlag, trafficToPct } fro
|
|
|
8
8
|
import { withInflightTimeout } from "../sdk/inflightTimeout";
|
|
9
9
|
import { normalizeUrlsInObject } from "../sdk/normalizeUrls";
|
|
10
10
|
import { stripTrackingParams } from "../sdk/urlUtils";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_DEFERRED_ROOT_MARGIN,
|
|
13
|
+
DEFAULT_DEFERRED_TRIGGER,
|
|
14
|
+
type DeferredTrigger,
|
|
15
|
+
} from "./deferredTrigger";
|
|
12
16
|
import { findPageByPath, loadBlocks } from "./loader";
|
|
13
17
|
import { getOnBeforeResolveProps, getSection, registerOnBeforeResolveProps } from "./registry";
|
|
14
18
|
import {
|
|
@@ -148,6 +152,13 @@ export interface AsyncRenderingConfig {
|
|
|
148
152
|
* @default "intersection"
|
|
149
153
|
*/
|
|
150
154
|
deferredTrigger?: DeferredTrigger;
|
|
155
|
+
/**
|
|
156
|
+
* `rootMargin` of the observer behind `deferredTrigger: "intersection"`.
|
|
157
|
+
* Read in the browser, so the same "must run in client-loaded code" caveat
|
|
158
|
+
* as `deferredTrigger` applies. Only `@decocms/tanstack` reads it.
|
|
159
|
+
* @default "300px"
|
|
160
|
+
*/
|
|
161
|
+
deferredRootMargin?: string;
|
|
151
162
|
/**
|
|
152
163
|
* Fold threshold: sections at or above this flat index are DEFERRED
|
|
153
164
|
* (rendered as a skeleton and loaded on scroll), so their resolved props are
|
|
@@ -229,6 +240,7 @@ export function setAsyncRenderingConfig(config?: {
|
|
|
229
240
|
alwaysEager?: string[];
|
|
230
241
|
respectCmsLazy?: boolean;
|
|
231
242
|
deferredTrigger?: DeferredTrigger;
|
|
243
|
+
deferredRootMargin?: string;
|
|
232
244
|
botAwareSeo?: boolean;
|
|
233
245
|
}): void {
|
|
234
246
|
const existing = getAsyncConfig();
|
|
@@ -237,6 +249,8 @@ export function setAsyncRenderingConfig(config?: {
|
|
|
237
249
|
respectCmsLazy: config?.respectCmsLazy ?? existing?.respectCmsLazy ?? true,
|
|
238
250
|
deferredTrigger:
|
|
239
251
|
config?.deferredTrigger ?? existing?.deferredTrigger ?? DEFAULT_DEFERRED_TRIGGER,
|
|
252
|
+
deferredRootMargin:
|
|
253
|
+
config?.deferredRootMargin ?? existing?.deferredRootMargin ?? DEFAULT_DEFERRED_ROOT_MARGIN,
|
|
240
254
|
foldThreshold: config?.foldThreshold ?? existing?.foldThreshold ?? DEFAULT_FOLD_THRESHOLD,
|
|
241
255
|
alwaysEager: merged,
|
|
242
256
|
botAwareSeo: config?.botAwareSeo ?? existing?.botAwareSeo ?? false,
|
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
|
});
|