@decocms/blocks 7.78.2 → 7.79.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
|
@@ -111,6 +111,34 @@ it("hydrates another store and isolates namespace, site and deployment", async (
|
|
|
111
111
|
).toBeUndefined();
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
+
it("data stores key by dataScope and survive a deploy; build stores do not", async () => {
|
|
115
|
+
const storage = createKVCacheStorage(fakeKV());
|
|
116
|
+
const bind = (build: string, fn: () => unknown) =>
|
|
117
|
+
RequestContext.run(new Request("https://site.test/"), () => {
|
|
118
|
+
bindCacheStorage({
|
|
119
|
+
storage,
|
|
120
|
+
scope: `site:${build}`,
|
|
121
|
+
dataScope: "site:data:1",
|
|
122
|
+
waitUntil: (work) => pending.push(work),
|
|
123
|
+
});
|
|
124
|
+
return fn();
|
|
125
|
+
});
|
|
126
|
+
const data = createCacheStore<{ n: number }>("loaders", 200, undefined, undefined, "data");
|
|
127
|
+
const html = createCacheStore<{ n: number }>("responses");
|
|
128
|
+
await bind("build-A", async () => {
|
|
129
|
+
data.set(data.key("item"), { n: 1 }, Date.now() + 60_000);
|
|
130
|
+
html.set(html.key("item"), { n: 2 }, Date.now() + 60_000);
|
|
131
|
+
await flush();
|
|
132
|
+
});
|
|
133
|
+
clearLoaderCache();
|
|
134
|
+
const data2 = createCacheStore<{ n: number }>("loaders", 200, undefined, undefined, "data");
|
|
135
|
+
const html2 = createCacheStore<{ n: number }>("responses");
|
|
136
|
+
expect(await bind("build-B", () => data2.read(data2.key("item")))).toEqual({ n: 1 });
|
|
137
|
+
expect(await bind("build-B", () => html2.read(html2.key("item")))).toBeUndefined();
|
|
138
|
+
// Without dataScope a data store falls back to the build scope.
|
|
139
|
+
expect(await run(storage, "site:build-B", () => data2.read(data2.key("item")))).toBeUndefined();
|
|
140
|
+
});
|
|
141
|
+
|
|
114
142
|
it("does not switch bindings between concurrent requests", async () => {
|
|
115
143
|
const a = createMemoryCacheStorage();
|
|
116
144
|
const b = createMemoryCacheStorage();
|
package/src/sdk/cacheStorage.ts
CHANGED
|
@@ -11,6 +11,12 @@ export interface CacheStorageContext {
|
|
|
11
11
|
storage: CacheStorage | null;
|
|
12
12
|
/** Include site, deployment and content revision. Never use a tenant-wide global binding. */
|
|
13
13
|
scope: string;
|
|
14
|
+
/**
|
|
15
|
+
* Scope for build-independent DATA caches (`createFetchCache`): raw upstream
|
|
16
|
+
* responses, keyed with a site-chosen data version instead of the build hash
|
|
17
|
+
* and CMS revision so they survive deploys and publishes. Defaults to `scope`.
|
|
18
|
+
*/
|
|
19
|
+
dataScope?: string;
|
|
14
20
|
/** Bypass both memory and shared storage for previews or private requests. */
|
|
15
21
|
disabled?: boolean;
|
|
16
22
|
waitUntil?: (work: Promise<unknown>) => void;
|
|
@@ -186,6 +192,8 @@ export function createCacheStore<T>(
|
|
|
186
192
|
maxEntries = 200,
|
|
187
193
|
maxBytes = 4 * 1024 * 1024,
|
|
188
194
|
sizeOf?: (value: T) => number,
|
|
195
|
+
/** `"data"`: key by `dataScope` — only for values that don't depend on the build. */
|
|
196
|
+
kind: "build" | "data" = "build",
|
|
189
197
|
) {
|
|
190
198
|
const local = new Map<string, { value: T; expiresAt: number; bytes: number }>();
|
|
191
199
|
let bytes = 0;
|
|
@@ -225,7 +233,9 @@ export function createCacheStore<T>(
|
|
|
225
233
|
}
|
|
226
234
|
return {
|
|
227
235
|
key(raw: string): string {
|
|
228
|
-
|
|
236
|
+
const context = getCacheStorageContext();
|
|
237
|
+
const scope = (kind === "data" ? context?.dataScope : undefined) ?? context?.scope;
|
|
238
|
+
return JSON.stringify([scope ?? "local", namespace, raw]);
|
|
229
239
|
},
|
|
230
240
|
get,
|
|
231
241
|
async read(key: string): Promise<T | undefined> {
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
9
|
import { configureMeter, type MeterAdapter } from "../middleware/observability";
|
|
10
|
+
import { bindCacheStorage, createMemoryCacheStorage } from "./cacheStorage";
|
|
10
11
|
import { createFetchCache } from "./fetchCache";
|
|
12
|
+
import { RequestContext } from "./requestContext";
|
|
11
13
|
|
|
12
14
|
interface Counter {
|
|
13
15
|
name: string;
|
|
@@ -54,9 +56,7 @@ afterEach(() => {
|
|
|
54
56
|
});
|
|
55
57
|
|
|
56
58
|
const statuses = () =>
|
|
57
|
-
counters
|
|
58
|
-
.filter((c) => c.name === "deco.cache.requests")
|
|
59
|
-
.map((c) => c.labels?.["status"]);
|
|
59
|
+
counters.filter((c) => c.name === "deco.cache.requests").map((c) => c.labels?.["status"]);
|
|
60
60
|
|
|
61
61
|
describe("createFetchCache — behavior", () => {
|
|
62
62
|
it("cold call is a MISS, second call is a HIT with the cached body", async () => {
|
|
@@ -162,3 +162,45 @@ describe("createFetchCache — telemetry labels", () => {
|
|
|
162
162
|
).resolves.toEqual({});
|
|
163
163
|
});
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
describe("createFetchCache — shared storage scope", () => {
|
|
167
|
+
const pending: Promise<unknown>[] = [];
|
|
168
|
+
const bind = <T>(
|
|
169
|
+
storage: ReturnType<typeof createMemoryCacheStorage>,
|
|
170
|
+
scope: string,
|
|
171
|
+
dataScope: string | undefined,
|
|
172
|
+
fn: () => Promise<T>,
|
|
173
|
+
) =>
|
|
174
|
+
RequestContext.run(new Request("https://site.test/"), () => {
|
|
175
|
+
bindCacheStorage({ storage, scope, dataScope, waitUntil: (w) => pending.push(w) });
|
|
176
|
+
return fn();
|
|
177
|
+
});
|
|
178
|
+
const flush = () => Promise.all(pending.splice(0));
|
|
179
|
+
|
|
180
|
+
// A fresh cache instance per "deploy" so the local tier can't answer.
|
|
181
|
+
async function acrossDeploy(dataScope: string | undefined) {
|
|
182
|
+
const storage = createMemoryCacheStorage();
|
|
183
|
+
let calls = 0;
|
|
184
|
+
const doFetch = () => {
|
|
185
|
+
calls++;
|
|
186
|
+
return Promise.resolve(jsonResponse({ ok: true }));
|
|
187
|
+
};
|
|
188
|
+
const before = createFetchCache({ provider: "vtex", ...KNOBS });
|
|
189
|
+
await bind(storage, "site:build-A", dataScope, () => before.fetchWithCache("k", doFetch));
|
|
190
|
+
await flush();
|
|
191
|
+
const after = createFetchCache({ provider: "vtex", ...KNOBS });
|
|
192
|
+
const body = await bind(storage, "site:build-B", dataScope, () =>
|
|
193
|
+
after.fetchWithCache("k", doFetch),
|
|
194
|
+
);
|
|
195
|
+
await flush();
|
|
196
|
+
return { body, calls };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
it("survives a build change when dataScope is bound", async () => {
|
|
200
|
+
expect(await acrossDeploy("site:data:")).toEqual({ body: { ok: true }, calls: 1 });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("misses after a build change without dataScope", async () => {
|
|
204
|
+
expect(await acrossDeploy(undefined)).toEqual({ body: { ok: true }, calls: 2 });
|
|
205
|
+
});
|
|
206
|
+
});
|
package/src/sdk/fetchCache.ts
CHANGED
|
@@ -130,7 +130,13 @@ function withTimeout<T>(work: Promise<T>, ms: number, label: string): Promise<T>
|
|
|
130
130
|
export function createFetchCache(config: FetchCacheConfig): FetchCache {
|
|
131
131
|
const { provider, maxEntries, freshTtlMs, staleIfErrorMs, inflightBackstopMs } = config;
|
|
132
132
|
|
|
133
|
-
const store = createCacheStore<CacheEntry>(
|
|
133
|
+
const store = createCacheStore<CacheEntry>(
|
|
134
|
+
`fetch:${provider}`,
|
|
135
|
+
maxEntries,
|
|
136
|
+
undefined,
|
|
137
|
+
undefined,
|
|
138
|
+
"data",
|
|
139
|
+
);
|
|
134
140
|
const inflight = new Map<string, Promise<CacheEntry>>();
|
|
135
141
|
|
|
136
142
|
function freshTtlForStatus(status: number): number {
|