@decocms/tanstack 7.86.5 → 7.87.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/tanstack",
3
- "version": "7.86.5",
3
+ "version": "7.87.0",
4
4
  "type": "module",
5
5
  "description": "Deco framework binding for TanStack Start + Cloudflare Workers",
6
6
  "repository": {
@@ -26,9 +26,9 @@
26
26
  "lint:unused": "knip"
27
27
  },
28
28
  "dependencies": {
29
- "@decocms/blocks": "7.86.5",
30
- "@decocms/blocks-admin": "7.86.5",
31
- "@decocms/blocks-cli": "7.86.5"
29
+ "@decocms/blocks": "7.87.0",
30
+ "@decocms/blocks-admin": "7.87.0",
31
+ "@decocms/blocks-cli": "7.87.0"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@tanstack/react-query": ">=5.0.0",
@@ -108,6 +108,31 @@ describe("withSiteGlobals", () => {
108
108
  expect(mockedResolvePageSections).toHaveBeenCalledTimes(1);
109
109
  });
110
110
 
111
+ it("a joiner does not hang when the owner request was cancelled", async () => {
112
+ // Workers cancels the owner request's I/O and timers when its client
113
+ // disconnects: the shared flight never settles. The joiner must give up
114
+ // on it after its own timer and resolve on its own.
115
+ vi.useFakeTimers();
116
+ try {
117
+ mockedLoadBlocks.mockReturnValue({
118
+ site: { global: [{ __resolveType: "Analytics" }] },
119
+ });
120
+ mockedResolvePageSections
121
+ .mockImplementationOnce(() => new Promise(() => {}))
122
+ .mockResolvedValue([{ component: "A.tsx", props: {}, key: "k0" }]);
123
+
124
+ void resolveSiteGlobals();
125
+ const joiner = resolveSiteGlobals();
126
+ await vi.advanceTimersByTimeAsync(10_000);
127
+ const r = await joiner;
128
+
129
+ expect(r.resolvedSections).toEqual([{ component: "A.tsx", props: {}, key: "k0" }]);
130
+ expect(mockedResolvePageSections).toHaveBeenCalledTimes(2);
131
+ } finally {
132
+ vi.useRealTimers();
133
+ }
134
+ });
135
+
111
136
  it("caches across calls within TTL", async () => {
112
137
  mockedLoadBlocks.mockReturnValue({
113
138
  site: { global: [{ __resolveType: "Analytics" }] },
@@ -130,10 +155,7 @@ describe("withSiteGlobals", () => {
130
155
  const ctx = { path: "/farm-etc" } as any;
131
156
  await resolveSiteGlobals(ctx);
132
157
 
133
- expect(mockedResolvePageSections).toHaveBeenCalledWith(
134
- [{ __resolveType: "Alert" }],
135
- ctx,
136
- );
158
+ expect(mockedResolvePageSections).toHaveBeenCalledWith([{ __resolveType: "Alert" }], ctx);
137
159
  });
138
160
 
139
161
  it("caches per path — different routes don't share a matched variant", async () => {
@@ -208,12 +230,7 @@ describe("withSiteGlobals", () => {
208
230
 
209
231
  describe("dedupeGlobals", () => {
210
232
  it("returns empty when globals is empty", () => {
211
- expect(
212
- dedupeGlobals(
213
- [],
214
- [{ component: "Header.tsx", props: {}, key: "p0" }],
215
- ),
216
- ).toEqual([]);
233
+ expect(dedupeGlobals([], [{ component: "Header.tsx", props: {}, key: "p0" }])).toEqual([]);
217
234
  });
218
235
 
219
236
  it("drops globals whose component already appears in existing", () => {
@@ -221,9 +238,7 @@ describe("withSiteGlobals", () => {
221
238
  { component: "Theme.tsx", props: {}, key: "g0" },
222
239
  { component: "Session.tsx", props: {}, key: "g1" },
223
240
  ];
224
- const existing = [
225
- { component: "Session.tsx", props: { fromPage: true }, key: "p0" },
226
- ];
241
+ const existing = [{ component: "Session.tsx", props: { fromPage: true }, key: "p0" }];
227
242
 
228
243
  const result = dedupeGlobals(globals, existing);
229
244
  // Session dropped (already on page); Theme kept.
@@ -27,7 +27,12 @@
27
27
 
28
28
  import type { MatcherContext, ResolvedSection } from "@decocms/blocks/cms";
29
29
  import { loadBlocks, onChange, resolvePageSections } from "@decocms/blocks/cms";
30
- import { createCacheStore, getCacheStorageContext } from "@decocms/blocks/sdk/cacheStorage";
30
+ import {
31
+ cacheBackground,
32
+ createCacheStore,
33
+ getCacheStorageContext,
34
+ } from "@decocms/blocks/sdk/cacheStorage";
35
+ import { joinInflight } from "@decocms/blocks/sdk/inflightTimeout";
31
36
 
32
37
  // ---------------------------------------------------------------------------
33
38
  // Types
@@ -133,7 +138,19 @@ export async function resolveSiteGlobals(matcherCtx?: MatcherContext): Promise<{
133
138
  : cache.get(cacheKey);
134
139
  if (cached && cached.expiresAt > now) return cached;
135
140
  const inflightEntry = inflight.get(cacheKey);
136
- if (inflightEntry) return inflightEntry;
141
+ if (inflightEntry) {
142
+ // Bounded by THIS request's timer: if the owner request was cancelled, its
143
+ // I/O and timers died with it and the slot never settles (see joinInflight).
144
+ return joinInflight(
145
+ inflightEntry,
146
+ () => {
147
+ if (inflight.get(cacheKey) === inflightEntry) inflight.delete(cacheKey);
148
+ return resolveSiteGlobals(matcherCtx);
149
+ },
150
+ undefined,
151
+ `siteGlobals ${cacheKey}`,
152
+ );
153
+ }
137
154
 
138
155
  const site = readSiteBlock();
139
156
  if (!site) {
@@ -152,7 +169,9 @@ export async function resolveSiteGlobals(matcherCtx?: MatcherContext): Promise<{
152
169
  return empty;
153
170
  }
154
171
 
155
- const p = (async () => {
172
+ // Cleanup lives in an outer `.finally` (always async) so it can compare by
173
+ // identity: a stale owner must not evict a newer flight a joiner published.
174
+ const p: Promise<CacheEntry> = (async () => {
156
175
  try {
157
176
  const resolvedSections = await resolvePageSections(rawRefs, matcherCtx);
158
177
  const entry: CacheEntry = {
@@ -166,11 +185,13 @@ export async function resolveSiteGlobals(matcherCtx?: MatcherContext): Promise<{
166
185
  console.error("[site-globals] failed to resolve:", err);
167
186
  // Don't cache failures — let the next request retry.
168
187
  return { resolvedSections: [], rawRefs, expiresAt: 0 };
169
- } finally {
170
- inflight.delete(cacheKey);
171
188
  }
172
- })();
189
+ })().finally(() => {
190
+ if (inflight.get(cacheKey) === p) inflight.delete(cacheKey);
191
+ });
173
192
 
193
+ // Keep the shared work alive past its owner request so joiners get it.
194
+ cacheBackground(p);
174
195
  inflight.set(cacheKey, p);
175
196
  return p;
176
197
  }
@@ -447,8 +447,10 @@ export interface DecoWorkerEntryOptions {
447
447
  * A raw response doesn't depend on the code or the decofile; the new
448
448
  * code re-transforms it, so a deploy can't serve an old shape.
449
449
  *
450
- * `cachedLoader` results, HTML responses and sections stay per-build in both
451
- * modes: they hold code output. Segment/device and geo stay in every key.
450
+ * HTML responses, sections and `cachedLoader` results stay per-build in both
451
+ * modes: they hold code output. The one exception: under `"preserve"`, a
452
+ * `cachedLoader` that sets `dataVersion` is keyed like `fetch:*` and survives
453
+ * too. Segment/device and geo stay in every key.
452
454
  * Any other value falls back to `"invalidate"`.
453
455
  *
454
456
  * To drop preserved responses once, set `DECO_DATA_CACHE_PURGE` to a new value
@@ -2623,7 +2625,9 @@ export function createDecoWorkerEntry(
2623
2625
  // Loader/commerce cache purge (in-memory, this isolate only). Escape hatch
2624
2626
  // to invalidate loader results immediately after an out-of-band upstream
2625
2627
  // change (e.g. a Magento catalog sync) without waiting out the TTL. A
2626
- // redeploy is the global lever — loader cache keys are build-hash versioned.
2628
+ // redeploy is the global lever for loaders keyed by build hash; one with
2629
+ // `dataVersion` under DECO_DATA_CACHE_ON_DEPLOY=preserve survives it (bump
2630
+ // its `dataVersion`, or use DECO_DATA_CACHE_PURGE).
2627
2631
  if (url.pathname === "/_cache/purge-loaders" && request.method === "POST") {
2628
2632
  return handlePurgeLoaders(request, env);
2629
2633
  }