@decocms/blocks 7.81.5 → 7.82.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 +1 -1
- package/src/sdk/cachedLoader.test.ts +39 -0
- package/src/sdk/cachedLoader.ts +17 -1
package/package.json
CHANGED
|
@@ -119,4 +119,43 @@ describe("createCachedLoader", () => {
|
|
|
119
119
|
expect(loaderFn).toHaveBeenCalledTimes(2);
|
|
120
120
|
expect(getLoaderCacheStats().entries).toBe(0);
|
|
121
121
|
});
|
|
122
|
+
|
|
123
|
+
it("shouldCache=false returns the result without storing it", async () => {
|
|
124
|
+
const loaderFn = vi.fn(async () => ({ products: [] as number[] }));
|
|
125
|
+
const cached = createCachedLoader("t/should-cache-miss", loaderFn, {
|
|
126
|
+
policy: "stale-while-revalidate",
|
|
127
|
+
maxAge: 60_000,
|
|
128
|
+
shouldCache: (r) => (r as { products: number[] }).products.length > 0,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(await cached({})).toEqual({ products: [] });
|
|
132
|
+
await cached({}); // not stored: goes to origin again
|
|
133
|
+
|
|
134
|
+
expect(loaderFn).toHaveBeenCalledTimes(2);
|
|
135
|
+
expect(getLoaderCacheStats().entries).toBe(0);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("an uncacheable SWR refresh keeps the stale entry and retries on the next hit", async () => {
|
|
139
|
+
vi.useFakeTimers();
|
|
140
|
+
vi.setSystemTime(new Date("2026-05-18T16:00:00.000Z"));
|
|
141
|
+
const results = [[1], [], [2]];
|
|
142
|
+
const loaderFn = vi.fn(async () => ({ products: results.shift() ?? [] }));
|
|
143
|
+
const cached = createCachedLoader("t/should-cache-swr", loaderFn, {
|
|
144
|
+
policy: "stale-while-revalidate",
|
|
145
|
+
maxAge: 1_000,
|
|
146
|
+
shouldCache: (r) => (r as { products: number[] }).products.length > 0,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await cached({}); // MISS -> stores [1]
|
|
150
|
+
vi.setSystemTime(new Date("2026-05-18T16:00:05.000Z")); // now stale
|
|
151
|
+
expect(await cached({})).toEqual({ products: [1] }); // refresh returns [] -> not stored
|
|
152
|
+
await vi.runAllTimersAsync();
|
|
153
|
+
expect(await cached({})).toEqual({ products: [1] }); // still stale [1]; refresh runs again
|
|
154
|
+
await vi.runAllTimersAsync();
|
|
155
|
+
|
|
156
|
+
expect(loaderFn).toHaveBeenCalledTimes(3);
|
|
157
|
+
vi.setSystemTime(new Date("2026-05-18T16:00:05.500Z")); // within maxAge of the [2] write
|
|
158
|
+
expect(await cached({})).toEqual({ products: [2] });
|
|
159
|
+
vi.useRealTimers();
|
|
160
|
+
});
|
|
122
161
|
});
|
package/src/sdk/cachedLoader.ts
CHANGED
|
@@ -55,6 +55,15 @@ export interface CachedLoaderOptions {
|
|
|
55
55
|
staleWhileRevalidate?: number;
|
|
56
56
|
/** Key function to generate a cache key from loader props. Default: JSON.stringify. */
|
|
57
57
|
keyFn?: (props: unknown) => string;
|
|
58
|
+
/**
|
|
59
|
+
* Whether a resolved result is stored. Returning `false` hands the value to
|
|
60
|
+
* the caller without writing it — e.g. an empty search that may just be the
|
|
61
|
+
* index rebuilding, which would otherwise be served as "not found" for the
|
|
62
|
+
* whole window. On a background SWR refresh the stale entry is kept. Use this
|
|
63
|
+
* instead of throwing to skip the write: a throw is counted as a loader error.
|
|
64
|
+
* Default: every resolved result is stored.
|
|
65
|
+
*/
|
|
66
|
+
shouldCache?: (result: unknown) => boolean;
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
/**
|
|
@@ -270,6 +279,7 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
270
279
|
staleIfError = 0,
|
|
271
280
|
staleWhileRevalidate = 300_000,
|
|
272
281
|
keyFn = JSON.stringify,
|
|
282
|
+
shouldCache,
|
|
273
283
|
} = resolved;
|
|
274
284
|
|
|
275
285
|
const env = typeof globalThis.process !== "undefined" ? globalThis.process.env : undefined;
|
|
@@ -381,6 +391,11 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
381
391
|
// Skip the write if a purge cleared the cache mid-refresh — otherwise
|
|
382
392
|
// we'd re-insert pre-purge data the purge was meant to drop.
|
|
383
393
|
if (gen !== cacheGeneration) return;
|
|
394
|
+
// Uncacheable result: keep serving the stale entry, retry on the next hit.
|
|
395
|
+
if (shouldCache && !shouldCache(result)) {
|
|
396
|
+
entry.refreshing = false;
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
384
399
|
setCacheEntry(
|
|
385
400
|
cacheKey,
|
|
386
401
|
{
|
|
@@ -429,7 +444,8 @@ export function createCachedLoader<TProps, TResult>(
|
|
|
429
444
|
recordLoaderMetric(name, performance.now() - loaderStart, "MISS");
|
|
430
445
|
// Skip caching if a purge landed while this loader was in flight — still
|
|
431
446
|
// return the fresh value to the caller, just don't persist a raced entry.
|
|
432
|
-
|
|
447
|
+
// Same for a result the caller marked uncacheable.
|
|
448
|
+
if (gen === cacheGeneration && (!shouldCache || shouldCache(result))) {
|
|
433
449
|
setCacheEntry(
|
|
434
450
|
cacheKey,
|
|
435
451
|
{
|