@decocms/tanstack 7.66.3 → 7.66.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/tanstack",
3
- "version": "7.66.3",
3
+ "version": "7.66.5",
4
4
  "type": "module",
5
5
  "description": "Deco framework binding for TanStack Start + Cloudflare Workers",
6
6
  "repository": {
@@ -27,9 +27,9 @@
27
27
  "lint:unused": "knip"
28
28
  },
29
29
  "dependencies": {
30
- "@decocms/blocks": "7.66.3",
31
- "@decocms/blocks-admin": "7.66.3",
32
- "@decocms/blocks-cli": "7.66.3",
30
+ "@decocms/blocks": "7.66.5",
31
+ "@decocms/blocks-admin": "7.66.5",
32
+ "@decocms/blocks-cli": "7.66.5",
33
33
  "@deco-cx/warp-node": "^0.3.16",
34
34
  "fast-json-patch": "^3.1.0",
35
35
  "ws": "^8.18.0"
@@ -1,4 +1,8 @@
1
- import { registerCacheableSections, registerSectionLoader } from "@decocms/blocks/cms";
1
+ import {
2
+ registerCacheableSections,
3
+ registerSectionLoader,
4
+ runSectionLoaders,
5
+ } from "@decocms/blocks/cms";
2
6
  import type { ResolvedSection } from "@decocms/blocks/cms";
3
7
  import { describe, expect, it } from "vitest";
4
8
  import {
@@ -7,6 +11,7 @@ import {
7
11
  cmsRouteConfig,
8
12
  parseLoadCmsHomePageInput,
9
13
  parseLoadCmsPageInput,
14
+ enrichGlobals,
10
15
  runSectionLoadersWithSeo,
11
16
  } from "./cmsRoute";
12
17
 
@@ -102,6 +107,112 @@ describe("runSectionLoadersWithSeo (#355)", () => {
102
107
  });
103
108
  });
104
109
 
110
+ /**
111
+ * Regression guard: `site.global` / `site.pageSections` entries never went
112
+ * through `runSectionLoaders`. `resolveSiteGlobals()` resolves only their
113
+ * *props* (and SWR-caches that across visitors for 5 minutes), so a global
114
+ * section's `loader` export simply never ran — the component rendered with
115
+ * raw CMS props and every loader-derived field was `undefined`.
116
+ *
117
+ * The visible symptom on a real storefront: an analytics global that has to
118
+ * emit its event inline, in document order (before the page-view global's
119
+ * script), could not do so at all. Rewriting it as a client-side `useQuery`
120
+ * moved the event to after hydration, which reorders the dataLayer.
121
+ */
122
+ describe("enrichGlobals — site.global loaders run per request", () => {
123
+ it("runs a global section's loader with the real request", async () => {
124
+ const component = `test/sections/GlobalLoader-${Date.now()}.tsx`;
125
+ const seen: string[] = [];
126
+ registerSectionLoader(component, async (props, req) => {
127
+ seen.push(req.headers.get("cookie") ?? "");
128
+ return { ...props, loaded: true };
129
+ });
130
+
131
+ const global: ResolvedSection = { component, props: {}, key: component, index: 0 };
132
+ const request = new Request("https://store.com/", { headers: { cookie: "sid=abc" } });
133
+
134
+ const result = await enrichGlobals([global], [], request);
135
+
136
+ expect((result[0].props as any).loaded).toBe(true);
137
+ expect(seen).toEqual(["sid=abc"]);
138
+ });
139
+
140
+ it("dedupes before running loaders — a page-overridden global never pays for its loader", async () => {
141
+ const component = `test/sections/GlobalDedupe-${Date.now()}.tsx`;
142
+ let calls = 0;
143
+ registerSectionLoader(component, async (props) => {
144
+ calls++;
145
+ return props;
146
+ });
147
+
148
+ const global: ResolvedSection = { component, props: {}, key: component, index: 0 };
149
+ const pageSection: ResolvedSection = { component, props: {}, key: `${component}-page`, index: 1 };
150
+
151
+ const result = await enrichGlobals([global], [pageSection], new Request("https://store.com/"));
152
+
153
+ expect(result).toEqual([]);
154
+ expect(calls).toBe(0);
155
+ });
156
+
157
+ it("preserves global order and passes through globals with no registered loader", async () => {
158
+ const withLoader = `test/sections/GlobalA-${Date.now()}.tsx`;
159
+ const withoutLoader = `test/sections/GlobalB-${Date.now()}.tsx`;
160
+ registerSectionLoader(withLoader, async (props) => ({ ...props, loaded: true }));
161
+
162
+ const globals: ResolvedSection[] = [
163
+ { component: withLoader, props: {}, key: withLoader, index: 0 },
164
+ { component: withoutLoader, props: { raw: 1 }, key: withoutLoader, index: 1 },
165
+ ];
166
+
167
+ const result = await enrichGlobals(globals, [], new Request("https://store.com/"));
168
+
169
+ expect(result.map((s) => s.component)).toEqual([withLoader, withoutLoader]);
170
+ expect((result[0].props as any).loaded).toBe(true);
171
+ expect((result[1].props as any).raw).toBe(1);
172
+ });
173
+
174
+ it("is a no-op when every global is deduped away", async () => {
175
+ expect(await enrichGlobals([], [], new Request("https://store.com/"))).toEqual([]);
176
+ });
177
+
178
+ /**
179
+ * Both call sites dedupe against the page's RAW sections so the globals'
180
+ * loaders can run concurrently with the page's instead of behind them. That
181
+ * is only sound while `runSectionLoaders` leaves `section.component`
182
+ * untouched — `dedupeGlobals` reads nothing else. If a future change makes a
183
+ * loader rewrite `component` (or add/drop sections), the two dedupe targets
184
+ * stop agreeing and an overridden global would render twice. Lock it here.
185
+ */
186
+ it("dedupes identically against raw and loader-enriched page sections", async () => {
187
+ const shared = `test/sections/GlobalInvariant-${Date.now()}.tsx`;
188
+ const globalOnly = `test/sections/GlobalOnly-${Date.now()}.tsx`;
189
+ registerSectionLoader(shared, async (props) => ({ ...props, loaded: true }));
190
+ registerSectionLoader(globalOnly, async (props) => ({ ...props, loaded: true }));
191
+
192
+ const globals: ResolvedSection[] = [
193
+ { component: globalOnly, props: {}, key: globalOnly, index: 0 },
194
+ { component: shared, props: {}, key: shared, index: 1 },
195
+ ];
196
+ const rawPageSections: ResolvedSection[] = [
197
+ { component: shared, props: { sku: "1" }, key: `${shared}-page`, index: 0 },
198
+ ];
199
+
200
+ const request = new Request("https://store.com/");
201
+ const enrichedPageSections = await runSectionLoaders(rawPageSections, request);
202
+
203
+ // The loader must not have touched `component` — the whole premise.
204
+ expect(enrichedPageSections.map((s) => s.component)).toEqual(
205
+ rawPageSections.map((s) => s.component),
206
+ );
207
+
208
+ const viaRaw = await enrichGlobals(globals, rawPageSections, request);
209
+ const viaEnriched = await enrichGlobals(globals, enrichedPageSections, request);
210
+
211
+ expect(viaRaw.map((s) => s.component)).toEqual([globalOnly]);
212
+ expect(viaRaw.map((s) => s.component)).toEqual(viaEnriched.map((s) => s.component));
213
+ });
214
+ });
215
+
105
216
  // ---------------------------------------------------------------------------
106
217
  // Pending UI defaults
107
218
  // ---------------------------------------------------------------------------
@@ -176,6 +176,46 @@ export async function runSectionLoadersWithSeo(
176
176
  };
177
177
  }
178
178
 
179
+ /**
180
+ * Dedupe `site.global` / `site.pageSections` against the page's own sections,
181
+ * then run the survivors' loaders against the real per-request Request.
182
+ *
183
+ * `resolveSiteGlobals()` only resolves a global's *props* — and it SWR-caches
184
+ * that result for 5 minutes across every visitor. Until now nothing ever fed
185
+ * globals through `runSectionLoaders`, so a global section's `loader` export
186
+ * simply never ran: the component rendered with the raw CMS props and every
187
+ * loader-derived field arrived `undefined`. Sites worked around it by moving
188
+ * the work client-side (a `useQuery` over `/deco/invoke`), which changes both
189
+ * the timing and the SSR output — an analytics global that has to emit its
190
+ * event inline, in document order, cannot be rewritten that way at all.
191
+ *
192
+ * Loaders run per request and are NOT cached here; only the prop resolution
193
+ * behind `resolveSiteGlobals()` keeps its 5-minute cache. Dedupe runs first so
194
+ * a global that the page overrides never pays for its loader.
195
+ *
196
+ * Section-level caching still applies: a global registered via
197
+ * `registerLayoutSections` / `registerCacheableSections` goes through the same
198
+ * cache tiers as any other section inside `runSectionLoaders`.
199
+ *
200
+ * `pageSections` may be the page's RAW sections — it does not have to be the
201
+ * loader-enriched ones. `dedupeGlobals` reads only `section.component`, and no
202
+ * branch of `runSingleSectionLoaderImpl` ever rewrites `component` (every path
203
+ * returns `section` or `{ ...section, props }`, 1:1, never adding or dropping
204
+ * entries). Passing the raw list is what lets this run CONCURRENTLY with the
205
+ * page's own loaders instead of behind them.
206
+ *
207
+ * @internal exported for tests
208
+ */
209
+ export async function enrichGlobals(
210
+ globalSections: ResolvedSection[],
211
+ pageSections: ResolvedSection[],
212
+ request: Request,
213
+ ): Promise<ResolvedSection[]> {
214
+ const kept = dedupeGlobals(globalSections, pageSections);
215
+ if (kept.length === 0) return kept;
216
+ return runSectionLoaders(kept, request);
217
+ }
218
+
179
219
  async function loadCmsPageInternal(fullPath: string, resolveGlobals: boolean) {
180
220
  const [basePath] = fullPath.split("?");
181
221
  // On client-side navigation getRequestUrl() is the /_serverFn/... endpoint,
@@ -207,21 +247,33 @@ async function loadCmsPageInternal(fullPath: string, resolveGlobals: boolean) {
207
247
  // identical for SSR (F5) and SPA (<Link>) navigations — see #233 for the
208
248
  // previous SPA-breakage when `withSiteGlobals` ran client-side and saw an
209
249
  // empty client-bundled `blocks.gen.ts`.
210
- const [{ enrichedSections, enrichedSeoSection }, globals] = await Promise.all([
211
- runSectionLoadersWithSeo(page.resolvedSections, page.seoSection, request),
212
- resolveGlobals ? resolveSiteGlobals(matcherCtx) : Promise.resolve(EMPTY_GLOBALS),
213
- ]);
250
+ const [{ enrichedSections, enrichedSeoSection }, { globals, enrichedGlobals }] =
251
+ await Promise.all([
252
+ runSectionLoadersWithSeo(page.resolvedSections, page.seoSection, request),
253
+ (async () => {
254
+ const globals = resolveGlobals ? await resolveSiteGlobals(matcherCtx) : EMPTY_GLOBALS;
255
+ return {
256
+ globals,
257
+ // Deduped against the page's RAW sections on purpose — see enrichGlobals.
258
+ // Waiting for `enrichedSections` would put the globals' loaders behind
259
+ // the page's on the TTFB critical path for no benefit.
260
+ enrichedGlobals: await enrichGlobals(
261
+ globals.resolvedSections,
262
+ page.resolvedSections,
263
+ request,
264
+ ),
265
+ };
266
+ })(),
267
+ ]);
214
268
 
215
269
  // After the loaders — a loader-resolved experiment assignment must be in the
216
270
  // cookie this response sets. See persistFlags.
217
271
  const flags = persistFlags(matcherCtx, takeExperimentAssignments());
218
272
 
219
- // Page sections take precedence over globals — dedupe drops any global
220
- // whose component is already rendered by the page.
221
- const mergedSections: ResolvedSection[] = [
222
- ...dedupeGlobals(globals.resolvedSections, enrichedSections),
223
- ...enrichedSections,
224
- ];
273
+ // Page sections take precedence over globals — dedupe drops any global whose
274
+ // component is already rendered by the page. The survivors ran their own
275
+ // loaders above, concurrently with the page's (see enrichGlobals).
276
+ const mergedSections: ResolvedSection[] = [...enrichedGlobals, ...enrichedSections];
225
277
 
226
278
  // Pre-import eager section modules so their default exports are cached
227
279
  // in resolvedComponents. This ensures SSR renders with direct component
@@ -345,16 +397,24 @@ export const loadCmsHomePage = createServerFn({ method: "GET" })
345
397
  };
346
398
  const page = await resolveDecoPage("/", matcherCtx);
347
399
  if (!page) return null;
348
- const [{ enrichedSections, enrichedSeoSection }, globals] = await Promise.all([
349
- runSectionLoadersWithSeo(page.resolvedSections, page.seoSection, request),
350
- resolveGlobals ? resolveSiteGlobals(matcherCtx) : Promise.resolve(EMPTY_GLOBALS),
351
- ]);
400
+ const [{ enrichedSections, enrichedSeoSection }, { globals, enrichedGlobals }] =
401
+ await Promise.all([
402
+ runSectionLoadersWithSeo(page.resolvedSections, page.seoSection, request),
403
+ (async () => {
404
+ const globals = resolveGlobals ? await resolveSiteGlobals(matcherCtx) : EMPTY_GLOBALS;
405
+ return {
406
+ globals,
407
+ enrichedGlobals: await enrichGlobals(
408
+ globals.resolvedSections,
409
+ page.resolvedSections,
410
+ request,
411
+ ),
412
+ };
413
+ })(),
414
+ ]);
352
415
  const flags = persistFlags(matcherCtx, takeExperimentAssignments());
353
416
 
354
- const mergedSections: ResolvedSection[] = [
355
- ...dedupeGlobals(globals.resolvedSections, enrichedSections),
356
- ...enrichedSections,
357
- ];
417
+ const mergedSections: ResolvedSection[] = [...enrichedGlobals, ...enrichedSections];
358
418
 
359
419
  const eagerKeys = mergedSections.map((s) => s.component);
360
420
  await preloadSectionComponents(eagerKeys);