@decocms/blocks 7.80.2 → 7.81.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 +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/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,
|