@decocms/blocks 8.1.0-next.0 → 8.1.0-next.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.
Files changed (114) hide show
  1. package/package.json +6 -92
  2. package/src/index.ts +2 -12
  3. package/src/protocol/storage/memory.ts +1 -1
  4. package/src/v8/background.ts +3 -3
  5. package/src/v8/builtins/legacy.ts +1 -1
  6. package/src/v8/cli/index.ts +1 -1
  7. package/src/v8/fetch.ts +8 -27
  8. package/src/v8/remoteLoader.ts +1 -1
  9. package/src/v8/telemetry.ts +1 -1
  10. package/src/cms/applySectionConventions.ts +0 -148
  11. package/src/cms/blockSource.ts +0 -228
  12. package/src/cms/client.ts +0 -85
  13. package/src/cms/deferredTrigger.ts +0 -58
  14. package/src/cms/draftSource.ts +0 -476
  15. package/src/cms/index.ts +0 -180
  16. package/src/cms/loadDecofileDirectory.ts +0 -89
  17. package/src/cms/loader.ts +0 -563
  18. package/src/cms/registry.ts +0 -264
  19. package/src/cms/renderJson.ts +0 -179
  20. package/src/cms/resolve.ts +0 -2583
  21. package/src/cms/schema.ts +0 -1219
  22. package/src/cms/sectionLoaderContext.ts +0 -113
  23. package/src/cms/sectionLoaders.ts +0 -686
  24. package/src/cms/sectionMixins.ts +0 -153
  25. package/src/flags/audience.ts +0 -56
  26. package/src/flags/everyone.ts +0 -29
  27. package/src/flags/flag.ts +0 -15
  28. package/src/flags/multivariate/image.ts +0 -11
  29. package/src/flags/multivariate/message.ts +0 -11
  30. package/src/flags/multivariate/page.ts +0 -16
  31. package/src/flags/multivariate/section.ts +0 -16
  32. package/src/flags/multivariate.ts +0 -1
  33. package/src/flags/types.ts +0 -59
  34. package/src/flags/utils/multivariate.ts +0 -20
  35. package/src/hooks/Image.tsx +0 -314
  36. package/src/hooks/JsonLd.tsx +0 -353
  37. package/src/hooks/LazySection.tsx +0 -121
  38. package/src/hooks/LiveControls.tsx +0 -144
  39. package/src/hooks/Picture.tsx +0 -127
  40. package/src/hooks/RenderSection.tsx +0 -91
  41. package/src/hooks/SectionErrorFallback.tsx +0 -97
  42. package/src/hooks/Stats.tsx +0 -134
  43. package/src/hooks/index.ts +0 -35
  44. package/src/hooks/useLoadMore.ts +0 -100
  45. package/src/matchers/builtins.ts +0 -459
  46. package/src/matchers/countryNames.ts +0 -104
  47. package/src/matchers/override.ts +0 -136
  48. package/src/matchers/posthog.ts +0 -154
  49. package/src/middleware/decoState.ts +0 -55
  50. package/src/middleware/healthMetrics.ts +0 -133
  51. package/src/middleware/hydrationContext.ts +0 -79
  52. package/src/middleware/index.ts +0 -88
  53. package/src/middleware/liveness.ts +0 -21
  54. package/src/middleware/observability.ts +0 -800
  55. package/src/middleware/validateSection.ts +0 -100
  56. package/src/preview/DraftPreviewBadge.tsx +0 -306
  57. package/src/preview/decoMark.ts +0 -11
  58. package/src/preview/index.ts +0 -8
  59. package/src/sdk/abTesting.ts +0 -556
  60. package/src/sdk/analytics.ts +0 -147
  61. package/src/sdk/cacheHeaders.ts +0 -633
  62. package/src/sdk/cacheStorage.ts +0 -287
  63. package/src/sdk/cachedLoader.ts +0 -556
  64. package/src/sdk/clx.ts +0 -5
  65. package/src/sdk/cn.ts +0 -28
  66. package/src/sdk/composite.ts +0 -114
  67. package/src/sdk/cookie.ts +0 -129
  68. package/src/sdk/crypto.ts +0 -199
  69. package/src/sdk/csp.ts +0 -59
  70. package/src/sdk/deepOmit.ts +0 -52
  71. package/src/sdk/detectDevice.ts +0 -45
  72. package/src/sdk/djb2.ts +0 -20
  73. package/src/sdk/encoding.ts +0 -44
  74. package/src/sdk/env.ts +0 -54
  75. package/src/sdk/experiments.ts +0 -314
  76. package/src/sdk/fetchCache.ts +0 -269
  77. package/src/sdk/fetchTimeout.ts +0 -58
  78. package/src/sdk/flags.ts +0 -138
  79. package/src/sdk/http.ts +0 -124
  80. package/src/sdk/index.ts +0 -97
  81. package/src/sdk/inflightTimeout.ts +0 -53
  82. package/src/sdk/instrumentedFetch.ts +0 -362
  83. package/src/sdk/invoke.ts +0 -298
  84. package/src/sdk/logger.ts +0 -304
  85. package/src/sdk/mergeCacheControl.ts +0 -150
  86. package/src/sdk/nonce.ts +0 -46
  87. package/src/sdk/normalizeUrls.ts +0 -91
  88. package/src/sdk/observability.ts +0 -116
  89. package/src/sdk/otel.ts +0 -1118
  90. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  91. package/src/sdk/otelAdapters.ts +0 -144
  92. package/src/sdk/otelHttpLog.ts +0 -419
  93. package/src/sdk/otelHttpMeter.ts +0 -532
  94. package/src/sdk/otelHttpTracer.ts +0 -543
  95. package/src/sdk/redirects.ts +0 -336
  96. package/src/sdk/requestContext.ts +0 -281
  97. package/src/sdk/requestContextStorage.browser.ts +0 -43
  98. package/src/sdk/requestContextStorage.ts +0 -35
  99. package/src/sdk/responseCache.ts +0 -82
  100. package/src/sdk/retry.ts +0 -45
  101. package/src/sdk/serverTimings.ts +0 -68
  102. package/src/sdk/signal.ts +0 -42
  103. package/src/sdk/sitemap.ts +0 -160
  104. package/src/sdk/urlRedaction.ts +0 -82
  105. package/src/sdk/urlUtils.ts +0 -183
  106. package/src/sdk/useDevice.ts +0 -89
  107. package/src/sdk/useDeviceContext.tsx +0 -108
  108. package/src/sdk/useId.ts +0 -7
  109. package/src/sdk/useScript.ts +0 -210
  110. package/src/sdk/useSuggestions.ts +0 -188
  111. package/src/sdk/wrapCaughtErrors.ts +0 -107
  112. package/src/setup.ts +0 -120
  113. package/src/types/index.ts +0 -53
  114. package/src/types/widgets.ts +0 -14
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "8.1.0-next.0",
3
+ "version": "8.1.0-next.1",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
7
7
  },
8
- "description": "Deco framework-agnostic core: CMS block resolution, section registry, matchers, portable SDK utilities",
8
+ "description": "Deco CMS: content resolution, built-in blocks, the deco CLI and the content protocol",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "https://github.com/decocms/blocks.git",
@@ -28,13 +28,9 @@
28
28
  "exports": {
29
29
  ".": "./src/index.ts",
30
30
  "./analytics": "./src/v8/analytics.ts",
31
- "./cms": "./src/cms/index.ts",
32
- "./cms/client": "./src/cms/client.ts",
33
- "./cms/loadDecofileDirectory": "./src/cms/loadDecofileDirectory.ts",
34
31
  "./cli": "./src/v8/cli/index.ts",
35
32
  "./fetch": "./src/v8/fetch.ts",
36
- "./hooks": "./src/hooks/index.ts",
37
- "./preview": "./src/preview/index.ts",
33
+ "./secrets": "./src/v8/secrets.ts",
38
34
  "./protocol": "./src/protocol/index.ts",
39
35
  "./protocol/keys": "./src/protocol/keys.ts",
40
36
  "./protocol/server": "./src/protocol/server/index.ts",
@@ -42,109 +38,27 @@
42
38
  "node": "./src/protocol/storage/fs/index.ts",
43
39
  "default": "./src/protocol/storage/fs/unsupported.ts"
44
40
  },
45
- "./protocol/conformance": "./src/protocol/conformance/index.ts",
46
- "./secrets": "./src/v8/secrets.ts",
47
- "./types": "./src/types/index.ts",
48
- "./types/widgets": "./src/types/widgets.ts",
49
- "./setup": "./src/setup.ts",
50
- "./matchers/builtins": "./src/matchers/builtins.ts",
51
- "./matchers/posthog": "./src/matchers/posthog.ts",
52
- "./matchers/override": "./src/matchers/override.ts",
53
- "./flags/audience": "./src/flags/audience.ts",
54
- "./flags/everyone": "./src/flags/everyone.ts",
55
- "./flags/flag": "./src/flags/flag.ts",
56
- "./flags/types": "./src/flags/types.ts",
57
- "./flags/multivariate": "./src/flags/multivariate.ts",
58
- "./flags/multivariate/image": "./src/flags/multivariate/image.ts",
59
- "./flags/multivariate/message": "./src/flags/multivariate/message.ts",
60
- "./flags/multivariate/page": "./src/flags/multivariate/page.ts",
61
- "./flags/multivariate/section": "./src/flags/multivariate/section.ts",
62
- "./middleware": "./src/middleware/index.ts",
63
- "./middleware/healthMetrics": "./src/middleware/healthMetrics.ts",
64
- "./middleware/hydrationContext": "./src/middleware/hydrationContext.ts",
65
- "./middleware/observability": "./src/middleware/observability.ts",
66
- "./middleware/validateSection": "./src/middleware/validateSection.ts",
67
- "./sdk": "./src/sdk/index.ts",
68
- "./sdk/abTesting": "./src/sdk/abTesting.ts",
69
- "./sdk/analytics": "./src/sdk/analytics.ts",
70
- "./sdk/cachedLoader": "./src/sdk/cachedLoader.ts",
71
- "./sdk/responseCache": "./src/sdk/responseCache.ts",
72
- "./sdk/cacheStorage": "./src/sdk/cacheStorage.ts",
73
- "./sdk/cacheHeaders": "./src/sdk/cacheHeaders.ts",
74
- "./sdk/clx": "./src/sdk/clx.ts",
75
- "./sdk/cn": "./src/sdk/cn.ts",
76
- "./sdk/composite": "./src/sdk/composite.ts",
77
- "./sdk/cookie": "./src/sdk/cookie.ts",
78
- "./sdk/crypto": "./src/sdk/crypto.ts",
79
- "./sdk/csp": "./src/sdk/csp.ts",
80
- "./sdk/detectDevice": "./src/sdk/detectDevice.ts",
81
- "./sdk/djb2": "./src/sdk/djb2.ts",
82
- "./sdk/encoding": "./src/sdk/encoding.ts",
83
- "./sdk/env": "./src/sdk/env.ts",
84
- "./sdk/fetchCache": "./src/sdk/fetchCache.ts",
85
- "./sdk/fetchTimeout": "./src/sdk/fetchTimeout.ts",
86
- "./sdk/experiments": "./src/sdk/experiments.ts",
87
- "./sdk/flags": "./src/sdk/flags.ts",
88
- "./sdk/http": "./src/sdk/http.ts",
89
- "./sdk/instrumentedFetch": "./src/sdk/instrumentedFetch.ts",
90
- "./sdk/invoke": "./src/sdk/invoke.ts",
91
- "./sdk/logger": "./src/sdk/logger.ts",
92
- "./sdk/mergeCacheControl": "./src/sdk/mergeCacheControl.ts",
93
- "./sdk/nonce": "./src/sdk/nonce.ts",
94
- "./sdk/normalizeUrls": "./src/sdk/normalizeUrls.ts",
95
- "./sdk/observability": "./src/sdk/observability.ts",
96
- "./sdk/otel": "./src/sdk/otel.ts",
97
- "./sdk/otelAdapters": "./src/sdk/otelAdapters.ts",
98
- "./sdk/otelAdapters/clickhouseCollector": "./src/sdk/otelAdapters/clickhouseCollector.ts",
99
- "./sdk/otelHttpLog": "./src/sdk/otelHttpLog.ts",
100
- "./sdk/otelHttpMeter": "./src/sdk/otelHttpMeter.ts",
101
- "./sdk/otelHttpTracer": "./src/sdk/otelHttpTracer.ts",
102
- "./sdk/redirects": "./src/sdk/redirects.ts",
103
- "./sdk/requestContext": "./src/sdk/requestContext.ts",
104
- "./sdk/requestContextStorage": {
105
- "workerd": "./src/sdk/requestContextStorage.ts",
106
- "node": "./src/sdk/requestContextStorage.ts",
107
- "browser": "./src/sdk/requestContextStorage.browser.ts",
108
- "default": "./src/sdk/requestContextStorage.ts"
109
- },
110
- "./sdk/retry": "./src/sdk/retry.ts",
111
- "./sdk/serverTimings": "./src/sdk/serverTimings.ts",
112
- "./sdk/signal": "./src/sdk/signal.ts",
113
- "./sdk/sitemap": "./src/sdk/sitemap.ts",
114
- "./sdk/urlUtils": "./src/sdk/urlUtils.ts",
115
- "./sdk/useDevice": "./src/sdk/useDevice.ts",
116
- "./sdk/useId": "./src/sdk/useId.ts",
117
- "./sdk/useScript": "./src/sdk/useScript.ts",
118
- "./sdk/useSuggestions": "./src/sdk/useSuggestions.ts",
119
- "./sdk/wrapCaughtErrors": "./src/sdk/wrapCaughtErrors.ts"
41
+ "./protocol/conformance": "./src/protocol/conformance/index.ts"
120
42
  },
121
43
  "scripts": {
122
44
  "build": "tsc",
123
45
  "test": "vitest run --root ../.. packages/blocks/",
124
- "typecheck": "tsc --noEmit",
125
- "lint:unused": "knip"
46
+ "typecheck": "tsc --noEmit"
126
47
  },
127
48
  "dependencies": {
128
- "@opentelemetry/api": "^1.9.1",
129
- "@opentelemetry/semantic-conventions": "^1.41.1",
130
- "@tanstack/store": "^0.9.1",
131
49
  "ajv": "^8.20.0",
132
- "clsx": "^2.1.1",
133
- "fast-json-patch": "^3.1.0",
134
- "tailwind-merge": "^3.3.1",
135
50
  "tsx": "^4.22.5",
136
51
  "zod": "^4.4.3"
137
52
  },
138
53
  "peerDependencies": {
139
54
  "react": "^19.0.0",
140
- "react-dom": "^19.0.0",
141
55
  "typescript": "^5.0.0"
142
56
  },
143
57
  "devDependencies": {
144
58
  "@types/react": "^19.0.0",
145
59
  "@types/react-dom": "^19.0.0",
146
60
  "esbuild": "^0.27.3",
147
- "knip": "^5.86.0",
61
+ "react-dom": "^19.0.0",
148
62
  "typescript": "^5.9.0"
149
63
  },
150
64
  "publishConfig": {
package/src/index.ts CHANGED
@@ -1,15 +1,5 @@
1
- // @decocms/blocks — framework-agnostic core
2
- export * from "./cms/index";
3
- export * from "./hooks/index";
4
- export * from "./middleware/index";
5
- // Observability surface — logger + instrumentWorker live behind their own
6
- // granular imports too (see `@decocms/blocks/sdk/logger`, `.../observability`).
7
- export { type Logger, type LogLevel, logger, setLogLevel } from "./sdk/logger";
8
- export * from "./types/index";
9
-
10
- // Next major (v8) core SDK. Explicit re-exports shadow the star exports above,
11
- // so where v7 and v8 share a name (`DraftPointer`, `parseDraftPointer`) the
12
- // root serves the v8 one; v7's stays reachable at `@decocms/blocks/cms`.
1
+ // @decocms/blocks: the v8 runtime API. The CLI, protocol and helpers live on
2
+ // their own subpaths (`/cli`, `/protocol`, `/fetch`, `/analytics`, `/secrets`).
13
3
  export {
14
4
  type Analytics,
15
5
  type Block,
@@ -1,5 +1,5 @@
1
1
  /**
2
- * `@decocms/blocks/protocol/storage/memory`: an in-memory `ContentStorage`.
2
+ * An in-memory `ContentStorage` (internal; not a package subpath).
3
3
  *
4
4
  * A reference implementation of the storage contract, including durable
5
5
  * request-key receipts persisted atomically with each commit, and a fixture
@@ -4,7 +4,7 @@
4
4
  * /next/telemetry-internals#sending-in-the-background).
5
5
  *
6
6
  * Where it runs, in order:
7
- * - a host hook a framework binding installs under {@link BACKGROUND_HOOK};
7
+ * - a hook the host installs under {@link BACKGROUND_HOOK};
8
8
  * - on Cloudflare Workers, which run no timers between requests, the
9
9
  * platform's `waitUntil` (from `cloudflare:workers`), so the task runs
10
10
  * after the response with no binding and no user code: the core installs
@@ -17,7 +17,7 @@
17
17
  * A task never throws into its caller: errors and rejections are swallowed.
18
18
  */
19
19
 
20
- /** Where a binding installs `(task) => void` to run background work its own way. */
20
+ /** Where a host installs `(task) => void` to run background work its own way. */
21
21
  const BACKGROUND_HOOK = Symbol.for("decocms.blocks.background");
22
22
 
23
23
  type Task = () => unknown;
@@ -101,7 +101,7 @@ export function later(ms: number, callback: () => void): void {
101
101
  timer?.unref?.();
102
102
  }
103
103
 
104
- /** Whether a binding installed a background hook (then batches go out after each response). */
104
+ /** Whether a host installed a background hook (then batches go out after each response). */
105
105
  export function hasBackgroundHook(): boolean {
106
106
  return typeof (globalThis as BackgroundGlobals)[BACKGROUND_HOOK] === "function";
107
107
  }
@@ -25,7 +25,7 @@ import { isPlainObject, type JsonObject } from "../json";
25
25
  * (/next/studio-compatibility#well-known-types-and-the-alias-table); other
26
26
  * legacy names (`$live/…`, the per-kind multivariate files, the date
27
27
  * matcher) are rewritten to these by the one-time migration
28
- * (`@decocms/blocks-migrate`), and types with no built-in counterpart are
28
+ * (the `deco-v7-to-v8-migration` skill), and types with no built-in counterpart are
29
29
  * left to the site's block map.
30
30
  */
31
31
  export const LEGACY_ALIASES: Readonly<Record<string, string>> = Object.freeze({
@@ -5,7 +5,7 @@
5
5
  * reaches an app bundle.
6
6
  *
7
7
  * The four commands and their options, plus what the one-time migration
8
- * (`@decocms/blocks-migrate`) reads: the built-in block names, the alias
8
+ * (the `deco-v7-to-v8-migration` skill) reads: the built-in block names, the alias
9
9
  * table and the saved-blocks reader.
10
10
  */
11
11
  export { BUILT_IN_BLOCKS, LEGACY_ALIASES } from "./builtins";
package/src/v8/fetch.ts CHANGED
@@ -14,7 +14,11 @@ import { currentTelemetry, describe, newTraceId } from "./telemetry";
14
14
  interface InstrumentedFetchOptions {
15
15
  /** The provider label, e.g. "vtex", "acme-search". */
16
16
  provider: string;
17
- /** The fetch underneath; defaults to `globalThis.fetch`. */
17
+ /**
18
+ * The fetch underneath; defaults to `globalThis.fetch`. An upstream cache
19
+ * goes here (see /next/caching#upstream-data); a response it serves from
20
+ * the cache carries `x-cache: HIT` and is measured with `cached=true`.
21
+ */
18
22
  fetch?: typeof fetch;
19
23
  /** Off unless set. `attempts` is how many times a failed request is retried; a retried request is measured once. */
20
24
  retry?: { attempts: number; backoffMs?: number };
@@ -27,19 +31,6 @@ type InstrumentedFetch = (
27
31
  init?: RequestInit & { operation?: string },
28
32
  ) => Promise<Response>;
29
33
 
30
- /**
31
- * Where a framework binding installs its upstream cache (on Workers,
32
- * `@decocms/tanstack` installs one over the Cache API). GETs go through it,
33
- * and it reports whether the response came from the cache.
34
- */
35
- const UPSTREAM_CACHE = Symbol.for("decocms.blocks.upstreamCache");
36
-
37
- type UpstreamCache = (
38
- input: string | URL | Request,
39
- init: RequestInit,
40
- fetch: typeof globalThis.fetch,
41
- ) => Promise<{ response: Response; cached: boolean }>;
42
-
43
34
  /** Statuses worth retrying: rate limits and gateway errors. */
44
35
  const RETRY_STATUSES = new Set([429, 502, 503, 504]);
45
36
  /** Only requests that are safe to repeat are retried. */
@@ -55,17 +46,7 @@ export function createInstrumentedFetch(options: InstrumentedFetchOptions): Inst
55
46
  const method = (
56
47
  requestInit.method ?? (input instanceof Request ? input.method : "GET")
57
48
  ).toUpperCase();
58
- const origin = options.fetch ?? globalThis.fetch;
59
- const cache = (globalThis as Record<symbol, UpstreamCache | undefined>)[UPSTREAM_CACHE];
60
- let cached = false;
61
- const doFetch: typeof globalThis.fetch =
62
- typeof cache === "function" && method === "GET"
63
- ? async (request, requestInit = {}) => {
64
- const hit = await cache(request as string | URL | Request, requestInit, origin);
65
- cached = hit.cached;
66
- return hit.response;
67
- }
68
- : origin;
49
+ const doFetch = options.fetch ?? globalThis.fetch;
69
50
  const maxRetries =
70
51
  retry && IDEMPOTENT.has(method) ? Math.max(0, Math.floor(retry.attempts)) : 0;
71
52
  const startedAt = Date.now();
@@ -112,8 +93,8 @@ export function createInstrumentedFetch(options: InstrumentedFetchOptions): Inst
112
93
  provider,
113
94
  operation,
114
95
  status_class: response ? `${Math.floor(response.status / 100)}xx` : "error",
115
- // Upstream caching lives in the binding, under this fetch.
116
- cached,
96
+ // An upstream cache is the site's own `fetch` option; it marks a hit `x-cache: HIT`.
97
+ cached: response?.headers.get("x-cache") === "HIT",
117
98
  retries,
118
99
  };
119
100
  telemetry.histogram(
@@ -89,7 +89,7 @@ class RemoteLoader implements Loader {
89
89
  const manifest = await this.#manifest();
90
90
  if (manifest.generation < this.#generation) return { updated: false };
91
91
 
92
- // Best effort: a fallback that can't load (a missing kvLoader key) is
92
+ // Best effort: a fallback that can't load (a KV key the deploy never wrote) is
93
93
  // fixed by downloading the release, not by failing the check.
94
94
  const fallbackRevision = await this.#fallbackRevisionNow().catch(() => undefined);
95
95
  const served = this.#current !== undefined ? this.#current.revision : fallbackRevision;
@@ -366,7 +366,7 @@ export class TelemetryPipeline {
366
366
  });
367
367
  return;
368
368
  }
369
- // With a binding's hook (Workers: after the response, in ctx.waitUntil),
369
+ // With a host hook (Workers: after the response, in ctx.waitUntil),
370
370
  // a batch goes out once it's FLUSH_MS old; a younger one stays pending
371
371
  // and a later request's measurement schedules it again.
372
372
  runInBackground(() => {
@@ -1,148 +0,0 @@
1
- /**
2
- * Applies convention-based section metadata generated by generate-sections.ts.
3
- *
4
- * Reads the generated sectionMeta/syncComponents/loadingFallbacks and calls
5
- * the appropriate registration functions — replacing manual calls to
6
- * registerSectionsSync, setAsyncRenderingConfig, registerCacheableSections,
7
- * registerLayoutSections, registerSeoSections, and registerSection.
8
- */
9
- import { registerSection, registerSectionsSync, setSectionRenderJson } from "./registry";
10
- import type { RenderJson } from "./renderJson";
11
- import {
12
- type AsyncRenderingConfig,
13
- getAsyncRenderingConfig,
14
- registerAlwaysDeferSections,
15
- registerEagerSections,
16
- registerNeverDeferSections,
17
- registerSeoSections,
18
- setAsyncRenderingConfig,
19
- } from "./resolve";
20
- import {
21
- type CacheableSectionInput,
22
- registerCacheableSections,
23
- registerLayoutSections,
24
- } from "./sectionLoaders";
25
-
26
- export interface SectionMetaEntry {
27
- eager?: boolean;
28
- neverDefer?: boolean;
29
- deferred?: boolean;
30
- cache?: string;
31
- layout?: boolean;
32
- sync?: boolean;
33
- clientOnly?: boolean;
34
- seo?: boolean;
35
- hasLoadingFallback?: boolean;
36
- /** `export const renderJson = false` — drop the section from ?renderJson. */
37
- renderJson?: false;
38
- /** `export const renderJson = (props) => ...` — a projection fn (in `renderJsons`). */
39
- hasRenderJson?: boolean;
40
- }
41
-
42
- export interface ApplySectionConventionsInput {
43
- /** Section metadata map from sections.gen.ts */
44
- meta: Record<string, SectionMetaEntry>;
45
- /** Sync-imported section modules from sections.gen.ts */
46
- syncComponents?: Record<string, any>;
47
- /** LoadingFallback components from sections.gen.ts */
48
- loadingFallbacks?: Record<string, React.ComponentType<any>>;
49
- /** renderJson projection functions from sections.gen.ts (?renderJson mobile path) */
50
- renderJsons?: Record<string, RenderJson>;
51
- /** Lazy section loaders from import.meta.glob (used for clientOnly/loadingFallback registration) */
52
- sectionGlob?: Record<string, () => Promise<any>>;
53
- }
54
-
55
- export function applySectionConventions(input: ApplySectionConventionsInput): void {
56
- const { meta, syncComponents, loadingFallbacks, renderJsons, sectionGlob } = input;
57
-
58
- const eagerSections: string[] = [];
59
- const neverDeferSections: string[] = [];
60
- const alwaysDeferSections: string[] = [];
61
- const layoutSections: string[] = [];
62
- const seoSections: string[] = [];
63
- const cacheableSections: Record<string, CacheableSectionInput> = {};
64
-
65
- for (const [key, entry] of Object.entries(meta)) {
66
- if (entry.eager) eagerSections.push(key);
67
- if (entry.neverDefer) neverDeferSections.push(key);
68
- if (entry.deferred) alwaysDeferSections.push(key);
69
- if (entry.layout) layoutSections.push(key);
70
- if (entry.seo) seoSections.push(key);
71
- if (entry.cache) cacheableSections[key] = entry.cache as CacheableSectionInput;
72
-
73
- if (entry.clientOnly && sectionGlob) {
74
- const globKey = sectionGlobKey(key, sectionGlob);
75
- if (globKey) {
76
- registerSection(key, sectionGlob[globKey] as any, { clientOnly: true });
77
- }
78
- }
79
-
80
- if (entry.hasLoadingFallback && loadingFallbacks?.[key] && sectionGlob) {
81
- const globKey = sectionGlobKey(key, sectionGlob);
82
- if (globKey) {
83
- registerSection(key, sectionGlob[globKey] as any, {
84
- loadingFallback: loadingFallbacks[key],
85
- });
86
- }
87
- }
88
-
89
- // renderJson (?renderJson mobile path): a `= false` opt-out drops the
90
- // section; a projection function trims its props. Set as a section option so
91
- // the serializer reads it via getSectionOptions without loading the module.
92
- if (entry.renderJson === false) {
93
- setSectionRenderJson(key, false);
94
- } else if (entry.hasRenderJson && renderJsons?.[key]) {
95
- setSectionRenderJson(key, renderJsons[key]);
96
- }
97
- }
98
-
99
- if (syncComponents && Object.keys(syncComponents).length > 0) {
100
- registerSectionsSync(syncComponents);
101
- }
102
-
103
- if (eagerSections.length > 0) {
104
- registerEagerSections(eagerSections);
105
- }
106
-
107
- if (neverDeferSections.length > 0) {
108
- registerNeverDeferSections(neverDeferSections);
109
- }
110
-
111
- if (alwaysDeferSections.length > 0) {
112
- registerAlwaysDeferSections(alwaysDeferSections);
113
- }
114
-
115
- // Always initialize asyncConfig so CMS Lazy deferral (`respectCmsLazy`) is
116
- // active — this is what makes the admin ⚡ toggle the source of truth. Without
117
- // it, asyncConfig stays null, `useAsync` is false in resolveDecoPage, and
118
- // even editor-marked ⚡ sections render eagerly. (The default foldThreshold is
119
- // Infinity, so position-based deferral stays off unless a site opts in.)
120
- const existing: Partial<AsyncRenderingConfig> = getAsyncRenderingConfig() ?? {};
121
- setAsyncRenderingConfig({
122
- ...existing,
123
- alwaysEager: [...(existing.alwaysEager ?? []), ...eagerSections],
124
- });
125
-
126
- if (layoutSections.length > 0) {
127
- registerLayoutSections(layoutSections);
128
- }
129
-
130
- if (seoSections.length > 0) {
131
- registerSeoSections(seoSections);
132
- }
133
-
134
- if (Object.keys(cacheableSections).length > 0) {
135
- registerCacheableSections(cacheableSections);
136
- }
137
- }
138
-
139
- function sectionGlobKey(
140
- sectionKey: string,
141
- glob: Record<string, () => Promise<any>>,
142
- ): string | null {
143
- const relative = sectionKey.replace("site/sections/", "./sections/");
144
- if (glob[relative]) return relative;
145
- const withDot = sectionKey.replace("site/", "./");
146
- if (glob[withDot]) return withDot;
147
- return null;
148
- }