@decocms/blocks 7.72.1 → 7.72.2
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/applySectionConventions.ts +9 -0
- package/src/cms/index.ts +1 -0
- package/src/cms/resolve.test.ts +96 -0
- package/src/cms/resolve.ts +63 -0
package/package.json
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
getAsyncRenderingConfig,
|
|
14
14
|
registerAlwaysDeferSections,
|
|
15
15
|
registerEagerSections,
|
|
16
|
+
registerFallbackPropsSections,
|
|
16
17
|
registerNeverDeferSections,
|
|
17
18
|
registerSeoSections,
|
|
18
19
|
setAsyncRenderingConfig,
|
|
@@ -33,6 +34,8 @@ export interface SectionMetaEntry {
|
|
|
33
34
|
clientOnly?: boolean;
|
|
34
35
|
seo?: boolean;
|
|
35
36
|
hasLoadingFallback?: boolean;
|
|
37
|
+
/** `export const fallbackProps = ["title"]` — props the skeleton may render. */
|
|
38
|
+
fallbackProps?: string[];
|
|
36
39
|
/** `export const renderJson = false` — drop the section from ?renderJson. */
|
|
37
40
|
renderJson?: false;
|
|
38
41
|
/** `export const renderJson = (props) => ...` — a projection fn (in `renderJsons`). */
|
|
@@ -61,6 +64,7 @@ export function applySectionConventions(input: ApplySectionConventionsInput): vo
|
|
|
61
64
|
const layoutSections: string[] = [];
|
|
62
65
|
const seoSections: string[] = [];
|
|
63
66
|
const cacheableSections: Record<string, CacheableSectionInput> = {};
|
|
67
|
+
const fallbackPropsSections: Record<string, string[]> = {};
|
|
64
68
|
|
|
65
69
|
for (const [key, entry] of Object.entries(meta)) {
|
|
66
70
|
if (entry.eager) eagerSections.push(key);
|
|
@@ -69,6 +73,7 @@ export function applySectionConventions(input: ApplySectionConventionsInput): vo
|
|
|
69
73
|
if (entry.layout) layoutSections.push(key);
|
|
70
74
|
if (entry.seo) seoSections.push(key);
|
|
71
75
|
if (entry.cache) cacheableSections[key] = entry.cache as CacheableSectionInput;
|
|
76
|
+
if (entry.fallbackProps?.length) fallbackPropsSections[key] = entry.fallbackProps;
|
|
72
77
|
|
|
73
78
|
if (entry.clientOnly && sectionGlob) {
|
|
74
79
|
const globKey = sectionGlobKey(key, sectionGlob);
|
|
@@ -134,6 +139,10 @@ export function applySectionConventions(input: ApplySectionConventionsInput): vo
|
|
|
134
139
|
if (Object.keys(cacheableSections).length > 0) {
|
|
135
140
|
registerCacheableSections(cacheableSections);
|
|
136
141
|
}
|
|
142
|
+
|
|
143
|
+
if (Object.keys(fallbackPropsSections).length > 0) {
|
|
144
|
+
registerFallbackPropsSections(fallbackPropsSections);
|
|
145
|
+
}
|
|
137
146
|
}
|
|
138
147
|
|
|
139
148
|
function sectionGlobKey(
|
package/src/cms/index.ts
CHANGED
package/src/cms/resolve.test.ts
CHANGED
|
@@ -33,12 +33,14 @@ import {
|
|
|
33
33
|
getAsyncRenderingConfig,
|
|
34
34
|
isDeferred,
|
|
35
35
|
isEagerRequest,
|
|
36
|
+
pickFallbackProps,
|
|
36
37
|
reExtractRawProps,
|
|
37
38
|
registerCommerceLoader,
|
|
38
39
|
registerMatcher,
|
|
39
40
|
resolveDeferred,
|
|
40
41
|
registerEagerSections,
|
|
41
42
|
registerAlwaysDeferSections,
|
|
43
|
+
registerFallbackPropsSections,
|
|
42
44
|
registerNeverDeferSections,
|
|
43
45
|
resolveDecoPage,
|
|
44
46
|
resolveDeferredSectionFull,
|
|
@@ -51,6 +53,43 @@ import {
|
|
|
51
53
|
} from "./resolve";
|
|
52
54
|
import { runSingleSectionLoader } from "./sectionLoaders";
|
|
53
55
|
|
|
56
|
+
describe("pickFallbackProps", () => {
|
|
57
|
+
it("keeps the declared primitives a LoadingFallback can render", () => {
|
|
58
|
+
expect(
|
|
59
|
+
pickFallbackProps({ galleryTitle: "Você pode ter perdido", columns: 4, disable: false }, [
|
|
60
|
+
"galleryTitle",
|
|
61
|
+
"columns",
|
|
62
|
+
"disable",
|
|
63
|
+
]),
|
|
64
|
+
).toEqual({ galleryTitle: "Você pode ter perdido", columns: 4, disable: false });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("ignores everything the section did not declare", () => {
|
|
68
|
+
const out = pickFallbackProps(
|
|
69
|
+
{
|
|
70
|
+
title: "Mais vendidos",
|
|
71
|
+
products: [{ id: "1" }, { id: "2" }],
|
|
72
|
+
apiToken: "plain-string-token",
|
|
73
|
+
richText: "x".repeat(5000),
|
|
74
|
+
},
|
|
75
|
+
["title"],
|
|
76
|
+
);
|
|
77
|
+
expect(out).toEqual({ title: "Mais vendidos" });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("drops a declared key holding an object graph — it would undo the payload saving", () => {
|
|
81
|
+
expect(
|
|
82
|
+
pickFallbackProps({ layout: { variation: "Full" }, title: "T" }, ["layout", "title"]),
|
|
83
|
+
).toEqual({ title: "T" });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("drops an empty string — it would beat `{ title = DEFAULT }` and blank the skeleton", () => {
|
|
87
|
+
expect(pickFallbackProps({ title: "", subtitle: "Assine" }, ["title", "subtitle"])).toEqual({
|
|
88
|
+
subtitle: "Assine",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
54
93
|
describe("resolveDeferredSectionFull", () => {
|
|
55
94
|
it("resolves a deferred section and preserves index", async () => {
|
|
56
95
|
const ds: DeferredSection = {
|
|
@@ -1490,3 +1529,60 @@ describe("asResolved — deferred props", () => {
|
|
|
1490
1529
|
}
|
|
1491
1530
|
});
|
|
1492
1531
|
});
|
|
1532
|
+
|
|
1533
|
+
// ---------------------------------------------------------------------------
|
|
1534
|
+
// fallbackProps — end to end through resolveDecoPage
|
|
1535
|
+
// ---------------------------------------------------------------------------
|
|
1536
|
+
|
|
1537
|
+
describe("resolveDecoPage — fallbackProps on the deferred object", () => {
|
|
1538
|
+
const HERO = "site/sections/Hero.tsx";
|
|
1539
|
+
const lazySec = {
|
|
1540
|
+
__resolveType: WELL_KNOWN_TYPES.LAZY,
|
|
1541
|
+
section: {
|
|
1542
|
+
__resolveType: HERO,
|
|
1543
|
+
galleryTitle: "Você pode ter perdido",
|
|
1544
|
+
columns: 4,
|
|
1545
|
+
apiToken: "plain-string-token",
|
|
1546
|
+
products: [{ id: "1" }],
|
|
1547
|
+
},
|
|
1548
|
+
};
|
|
1549
|
+
|
|
1550
|
+
beforeEach(() => {
|
|
1551
|
+
setAsyncRenderingConfig({ foldThreshold: Infinity, respectCmsLazy: true });
|
|
1552
|
+
(getSection as ReturnType<typeof vi.fn>).mockReturnValue({ default: () => null });
|
|
1553
|
+
(findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
1554
|
+
page: { name: "test", sections: [lazySec] },
|
|
1555
|
+
params: {},
|
|
1556
|
+
blockKey: "test-page",
|
|
1557
|
+
});
|
|
1558
|
+
});
|
|
1559
|
+
|
|
1560
|
+
afterEach(() => {
|
|
1561
|
+
registerFallbackPropsSections({ [HERO]: [] });
|
|
1562
|
+
(getSection as ReturnType<typeof vi.fn>).mockReset();
|
|
1563
|
+
(findPageByPath as ReturnType<typeof vi.fn>).mockReset();
|
|
1564
|
+
});
|
|
1565
|
+
|
|
1566
|
+
it("section declares nothing: neither rawProps nor fallbackProps are serialized", async () => {
|
|
1567
|
+
const page = await resolveDecoPage("/product/foo", {});
|
|
1568
|
+
const deferred = page!.deferredSections[0];
|
|
1569
|
+
expect(deferred).not.toHaveProperty("rawProps");
|
|
1570
|
+
expect(deferred).not.toHaveProperty("fallbackProps");
|
|
1571
|
+
});
|
|
1572
|
+
|
|
1573
|
+
it("section declares a prop: only that one travels, and rawProps still never survive", async () => {
|
|
1574
|
+
registerFallbackPropsSections({ [HERO]: ["galleryTitle"] });
|
|
1575
|
+
const page = await resolveDecoPage("/product/foo", {});
|
|
1576
|
+
const deferred = page!.deferredSections[0];
|
|
1577
|
+
expect(deferred).not.toHaveProperty("rawProps");
|
|
1578
|
+
expect(deferred.fallbackProps).toEqual({ galleryTitle: "Você pode ter perdido" });
|
|
1579
|
+
});
|
|
1580
|
+
|
|
1581
|
+
it("client nav behaves like SSR — the skeleton renders from the same projection", async () => {
|
|
1582
|
+
registerFallbackPropsSections({ [HERO]: ["galleryTitle"] });
|
|
1583
|
+
const nav = await resolveDecoPage("/product/foo", { isClientNavigation: true });
|
|
1584
|
+
const deferred = nav!.deferredSections[0];
|
|
1585
|
+
expect(deferred).not.toHaveProperty("rawProps");
|
|
1586
|
+
expect(deferred.fallbackProps).toEqual({ galleryTitle: "Você pode ter perdido" });
|
|
1587
|
+
});
|
|
1588
|
+
});
|
package/src/cms/resolve.ts
CHANGED
|
@@ -100,6 +100,12 @@ export interface DeferredSection {
|
|
|
100
100
|
*/
|
|
101
101
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
102
|
rawProps?: Record<string, any>;
|
|
103
|
+
/**
|
|
104
|
+
* The props the section declared via `export const fallbackProps`, so its
|
|
105
|
+
* `LoadingFallback` can render its CMS text before the real section arrives.
|
|
106
|
+
* Absent for a section that declared none.
|
|
107
|
+
*/
|
|
108
|
+
fallbackProps?: Record<string, string | number | boolean>;
|
|
103
109
|
}
|
|
104
110
|
|
|
105
111
|
// ---------------------------------------------------------------------------
|
|
@@ -301,6 +307,59 @@ function isAlwaysDeferSection(key: string): boolean {
|
|
|
301
307
|
return (G.__deco.alwaysDeferSectionKeys as Set<string> | undefined)?.has(key) ?? false;
|
|
302
308
|
}
|
|
303
309
|
|
|
310
|
+
// ---------------------------------------------------------------------------
|
|
311
|
+
// Fallback props — what a deferred section lets its skeleton render
|
|
312
|
+
// ---------------------------------------------------------------------------
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Register the props each section allows its `LoadingFallback` to render,
|
|
316
|
+
* declared as `export const fallbackProps = ["title"]`.
|
|
317
|
+
*
|
|
318
|
+
* `rawProps` are stripped before serialization (they can carry whole product
|
|
319
|
+
* lists), so the fallback rendered with `{}`: any CMS text in the skeleton was
|
|
320
|
+
* missing from the served HTML, and a skeleton that cannot render its own text
|
|
321
|
+
* cannot reserve the real section's height either. Deco on Fresh passed the
|
|
322
|
+
* resolved props to the fallback, so both worked there.
|
|
323
|
+
*
|
|
324
|
+
* Declaring is the opt-in, and it is per section rather than per site: nothing
|
|
325
|
+
* a section did not name reaches the client, so neither the payload nor a
|
|
326
|
+
* loader-only prop rides on a size heuristic. A section that declares nothing
|
|
327
|
+
* keeps rendering its skeleton with `{}`, exactly as before.
|
|
328
|
+
*/
|
|
329
|
+
export function registerFallbackPropsSections(entries: Record<string, string[]>): void {
|
|
330
|
+
G.__deco.fallbackPropKeys ??= new Map();
|
|
331
|
+
const map: Map<string, string[]> = G.__deco.fallbackPropKeys;
|
|
332
|
+
for (const [key, props] of Object.entries(entries)) map.set(key, props);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function getFallbackPropKeys(key: string): string[] | undefined {
|
|
336
|
+
return (G.__deco.fallbackPropKeys as Map<string, string[]> | undefined)?.get(key);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Project the declared props out of a deferred section's raw props.
|
|
341
|
+
*
|
|
342
|
+
* Still primitives only: a declared key holding an object or array would pull
|
|
343
|
+
* back the graph `rawProps` was stripped to avoid. Empty strings are dropped
|
|
344
|
+
* because a parameter default (`{ title = DEFAULT }`) only applies to
|
|
345
|
+
* `undefined`.
|
|
346
|
+
*/
|
|
347
|
+
export function pickFallbackProps(
|
|
348
|
+
rawProps: Record<string, unknown>,
|
|
349
|
+
keys: string[],
|
|
350
|
+
): Record<string, string | number | boolean> {
|
|
351
|
+
const out: Record<string, string | number | boolean> = {};
|
|
352
|
+
for (const key of keys) {
|
|
353
|
+
const value = rawProps[key];
|
|
354
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
355
|
+
out[key] = value;
|
|
356
|
+
} else if (typeof value === "string" && value.length > 0) {
|
|
357
|
+
out[key] = value;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
|
|
304
363
|
// ---------------------------------------------------------------------------
|
|
305
364
|
// Deferred rawProps cache — keeps rawProps server-side to trim HTML payload
|
|
306
365
|
// ---------------------------------------------------------------------------
|
|
@@ -2307,6 +2366,10 @@ async function resolveDecoPageImpl(
|
|
|
2307
2366
|
currentFlatIndex,
|
|
2308
2367
|
deferred.rawProps,
|
|
2309
2368
|
);
|
|
2369
|
+
const fallbackKeys = getFallbackPropKeys(deferred.component);
|
|
2370
|
+
if (fallbackKeys?.length) {
|
|
2371
|
+
deferred.fallbackProps = pickFallbackProps(deferred.rawProps, fallbackKeys);
|
|
2372
|
+
}
|
|
2310
2373
|
delete deferred.rawProps;
|
|
2311
2374
|
}
|
|
2312
2375
|
|