@decocms/start 0.30.4 → 0.31.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/index.ts +2 -1
- package/src/cms/registry.ts +37 -0
- package/src/cms/resolve.ts +36 -3
package/package.json
CHANGED
package/src/cms/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ export {
|
|
|
9
9
|
setBlocks,
|
|
10
10
|
withBlocksOverride,
|
|
11
11
|
} from "./loader";
|
|
12
|
-
export type { SectionModule, SectionOptions } from "./registry";
|
|
12
|
+
export type { OnBeforeResolveProps, SectionModule, SectionOptions } from "./registry";
|
|
13
13
|
export {
|
|
14
14
|
getResolvedComponent,
|
|
15
15
|
getSection,
|
|
@@ -19,6 +19,7 @@ export {
|
|
|
19
19
|
listRegisteredSections,
|
|
20
20
|
preloadSectionComponents,
|
|
21
21
|
preloadSectionModule,
|
|
22
|
+
registerOnBeforeResolveProps,
|
|
22
23
|
registerSection,
|
|
23
24
|
registerSections,
|
|
24
25
|
registerSectionsSync,
|
package/src/cms/registry.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type { ComponentType } from "react";
|
|
2
2
|
|
|
3
|
+
export type OnBeforeResolveProps = (
|
|
4
|
+
props: Record<string, unknown>,
|
|
5
|
+
) => Record<string, unknown>;
|
|
6
|
+
|
|
3
7
|
export type SectionModule = {
|
|
4
8
|
default: ComponentType<any>;
|
|
5
9
|
loader?: (props: any) => Promise<any> | any;
|
|
10
|
+
onBeforeResolveProps?: OnBeforeResolveProps;
|
|
6
11
|
LoadingFallback?: ComponentType<any>;
|
|
7
12
|
ErrorFallback?: ComponentType<{ error: Error }>;
|
|
8
13
|
};
|
|
@@ -30,6 +35,8 @@ if (!G.__deco.sectionRegistry) G.__deco.sectionRegistry = {};
|
|
|
30
35
|
if (!G.__deco.sectionOptions) G.__deco.sectionOptions = {};
|
|
31
36
|
if (!G.__deco.resolvedComponents) G.__deco.resolvedComponents = {};
|
|
32
37
|
if (!G.__deco.syncComponents) G.__deco.syncComponents = {};
|
|
38
|
+
if (!G.__deco.onBeforeResolvePropsRegistry)
|
|
39
|
+
G.__deco.onBeforeResolvePropsRegistry = {};
|
|
33
40
|
|
|
34
41
|
const registry: Record<string, RegistryEntry> = G.__deco.sectionRegistry;
|
|
35
42
|
const sectionOptions: Record<string, SectionOptions> = G.__deco.sectionOptions;
|
|
@@ -46,6 +53,13 @@ const resolvedComponents: Record<string, ComponentType<any>> = G.__deco.resolved
|
|
|
46
53
|
// These never need React.lazy/Suspense and render identically on SSR and hydration.
|
|
47
54
|
const syncComponents: Record<string, ComponentType<any>> = G.__deco.syncComponents;
|
|
48
55
|
|
|
56
|
+
// onBeforeResolveProps registry — functions that transform raw CMS props
|
|
57
|
+
// BEFORE resolvables are resolved. Allows sections to extract data from
|
|
58
|
+
// raw resolvable structures (e.g., collection IDs from loader refs) that
|
|
59
|
+
// would be lost after resolution.
|
|
60
|
+
const onBeforeResolvePropsRegistry: Record<string, OnBeforeResolveProps> =
|
|
61
|
+
G.__deco.onBeforeResolvePropsRegistry;
|
|
62
|
+
|
|
49
63
|
export function registerSection(key: string, loader: RegistryEntry, options?: SectionOptions) {
|
|
50
64
|
registry[key] = loader;
|
|
51
65
|
if (options) sectionOptions[key] = options;
|
|
@@ -129,6 +143,9 @@ export async function preloadSectionComponents(keys: string[]): Promise<void> {
|
|
|
129
143
|
if (mod?.default) {
|
|
130
144
|
resolvedComponents[key] = mod.default;
|
|
131
145
|
}
|
|
146
|
+
if (mod?.onBeforeResolveProps && !onBeforeResolvePropsRegistry[key]) {
|
|
147
|
+
onBeforeResolvePropsRegistry[key] = mod.onBeforeResolveProps;
|
|
148
|
+
}
|
|
132
149
|
const opts: SectionOptions = { ...sectionOptions[key] };
|
|
133
150
|
if (mod.LoadingFallback) opts.loadingFallback = mod.LoadingFallback;
|
|
134
151
|
if (mod.ErrorFallback) opts.errorFallback = mod.ErrorFallback;
|
|
@@ -206,6 +223,26 @@ export function getSyncComponent(key: string): ComponentType<any> | undefined {
|
|
|
206
223
|
return syncComponents[key];
|
|
207
224
|
}
|
|
208
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Register an onBeforeResolveProps function for a section.
|
|
228
|
+
* Called with raw CMS props (containing unresolved `__resolveType` references)
|
|
229
|
+
* BEFORE the resolution engine resolves them. Use to extract metadata from
|
|
230
|
+
* resolvable structures that would be lost after resolution.
|
|
231
|
+
*/
|
|
232
|
+
export function registerOnBeforeResolveProps(
|
|
233
|
+
sectionKey: string,
|
|
234
|
+
fn: OnBeforeResolveProps,
|
|
235
|
+
): void {
|
|
236
|
+
onBeforeResolvePropsRegistry[sectionKey] = fn;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Get the registered onBeforeResolveProps for a section, if any. */
|
|
240
|
+
export function getOnBeforeResolveProps(
|
|
241
|
+
sectionKey: string,
|
|
242
|
+
): OnBeforeResolveProps | undefined {
|
|
243
|
+
return onBeforeResolvePropsRegistry[sectionKey];
|
|
244
|
+
}
|
|
245
|
+
|
|
209
246
|
export function listRegisteredSections(): string[] {
|
|
210
247
|
return Object.keys(registry);
|
|
211
248
|
}
|
package/src/cms/resolve.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { findPageByPath, loadBlocks } from "./loader";
|
|
2
|
-
import { getSection } from "./registry";
|
|
2
|
+
import { getOnBeforeResolveProps, getSection, registerOnBeforeResolveProps } from "./registry";
|
|
3
3
|
import { isLayoutSection, runSingleSectionLoader } from "./sectionLoaders";
|
|
4
4
|
import { normalizeUrlsInObject } from "../sdk/normalizeUrls";
|
|
5
5
|
|
|
@@ -9,6 +9,30 @@ if (!G.__deco) G.__deco = {};
|
|
|
9
9
|
if (!G.__deco.commerceLoaders) G.__deco.commerceLoaders = {};
|
|
10
10
|
if (!G.__deco.customMatchers) G.__deco.customMatchers = {};
|
|
11
11
|
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// onBeforeResolveProps helper — eagerly loads the section module if needed
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
async function applyOnBeforeResolveProps(
|
|
17
|
+
sectionType: string,
|
|
18
|
+
props: Record<string, unknown>,
|
|
19
|
+
): Promise<Record<string, unknown>> {
|
|
20
|
+
let fn = getOnBeforeResolveProps(sectionType);
|
|
21
|
+
if (!fn) {
|
|
22
|
+
const loader = getSection(sectionType);
|
|
23
|
+
if (loader) {
|
|
24
|
+
try {
|
|
25
|
+
const mod = await loader();
|
|
26
|
+
if (mod?.onBeforeResolveProps) {
|
|
27
|
+
registerOnBeforeResolveProps(sectionType, mod.onBeforeResolveProps);
|
|
28
|
+
fn = mod.onBeforeResolveProps;
|
|
29
|
+
}
|
|
30
|
+
} catch {}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return fn ? fn(props) : props;
|
|
34
|
+
}
|
|
35
|
+
|
|
12
36
|
// ---------------------------------------------------------------------------
|
|
13
37
|
// Well-known resolve types — extracted as constants so they're searchable
|
|
14
38
|
// and overridable. Consumers migrating from deco-cx/deco may have blocks
|
|
@@ -479,7 +503,13 @@ async function internalResolve(value: unknown, rctx: ResolveContext): Promise<un
|
|
|
479
503
|
|
|
480
504
|
// Unknown type — resolve props but preserve __resolveType (it's a section)
|
|
481
505
|
const { __resolveType: _, ...rest } = obj;
|
|
482
|
-
|
|
506
|
+
|
|
507
|
+
// onBeforeResolveProps: let sections transform raw props before resolution.
|
|
508
|
+
// This runs with unresolved props (containing __resolveType refs) so sections
|
|
509
|
+
// can extract metadata that would be lost after resolution (e.g., collection IDs).
|
|
510
|
+
const propsToResolve = await applyOnBeforeResolveProps(resolveType, rest);
|
|
511
|
+
|
|
512
|
+
const resolvedRest = await resolveProps(propsToResolve, childCtx);
|
|
483
513
|
return { __resolveType: resolveType, ...resolvedRest };
|
|
484
514
|
}
|
|
485
515
|
|
|
@@ -1383,7 +1413,10 @@ export async function resolveDeferredSection(
|
|
|
1383
1413
|
depth: 0,
|
|
1384
1414
|
};
|
|
1385
1415
|
|
|
1386
|
-
|
|
1416
|
+
// onBeforeResolveProps: let sections transform raw props before resolution.
|
|
1417
|
+
const propsToResolve = await applyOnBeforeResolveProps(component, rawProps);
|
|
1418
|
+
|
|
1419
|
+
const resolvedProps = await resolveProps(propsToResolve, rctx);
|
|
1387
1420
|
const normalizedProps = normalizeNestedSections(resolvedProps) as Record<string, unknown>;
|
|
1388
1421
|
|
|
1389
1422
|
return {
|