@decocms/blocks 7.86.1 → 7.86.3
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/cms/loader.test.ts +31 -31
- package/src/cms/loader.ts +12 -20
package/package.json
CHANGED
package/src/cms/loader.test.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { computeRevision } from "./blockSource";
|
|
2
3
|
import { setDraftOverrideGetter } from "./draftSource";
|
|
3
4
|
import {
|
|
4
5
|
findPageByPath,
|
|
5
6
|
getAllPages,
|
|
7
|
+
getRevision,
|
|
6
8
|
loadBlocks,
|
|
7
9
|
matchPath,
|
|
8
10
|
setBlocks,
|
|
@@ -95,26 +97,17 @@ describe("matchPath", () => {
|
|
|
95
97
|
});
|
|
96
98
|
|
|
97
99
|
it("matches with an optional prefix before a literal segment", () => {
|
|
98
|
-
expect(
|
|
99
|
-
matchPath(
|
|
100
|
-
"/{acme/}?campanhas/*",
|
|
101
|
-
"/acme/campanhas/destaques-2023",
|
|
102
|
-
),
|
|
103
|
-
).toEqual({
|
|
100
|
+
expect(matchPath("/{acme/}?campanhas/*", "/acme/campanhas/destaques-2023")).toEqual({
|
|
104
101
|
"0": "destaques-2023",
|
|
105
102
|
});
|
|
106
|
-
expect(
|
|
107
|
-
matchPath("/{acme/}?campanhas/*", "/campanhas/destaques-2023"),
|
|
108
|
-
).toEqual({
|
|
103
|
+
expect(matchPath("/{acme/}?campanhas/*", "/campanhas/destaques-2023")).toEqual({
|
|
109
104
|
"0": "destaques-2023",
|
|
110
105
|
});
|
|
111
106
|
});
|
|
112
107
|
|
|
113
108
|
it("matches an optional suffix group present and absent", () => {
|
|
114
109
|
expect(matchPath("/black-friday{/70-off}?", "/black-friday")).toEqual({});
|
|
115
|
-
expect(
|
|
116
|
-
matchPath("/black-friday{/70-off}?", "/black-friday/70-off"),
|
|
117
|
-
).toEqual({});
|
|
110
|
+
expect(matchPath("/black-friday{/70-off}?", "/black-friday/70-off")).toEqual({});
|
|
118
111
|
});
|
|
119
112
|
});
|
|
120
113
|
|
|
@@ -134,9 +127,7 @@ describe("matchPath", () => {
|
|
|
134
127
|
// biome-ignore lint/performance/noDelete: restoring exact global state
|
|
135
128
|
delete g.URLPattern;
|
|
136
129
|
try {
|
|
137
|
-
expect(() => matchPath("/foo/:slug", "/foo/bar")).toThrow(
|
|
138
|
-
/URLPattern.*Node\.js >= 24/s,
|
|
139
|
-
);
|
|
130
|
+
expect(() => matchPath("/foo/:slug", "/foo/bar")).toThrow(/URLPattern.*Node\.js >= 24/s);
|
|
140
131
|
} finally {
|
|
141
132
|
if (saved !== undefined) g.URLPattern = saved;
|
|
142
133
|
}
|
|
@@ -432,9 +423,7 @@ describe("loadBlocks draft override — key percent-encoding", () => {
|
|
|
432
423
|
}));
|
|
433
424
|
|
|
434
425
|
// Exactly one page for "/" survives the merge — no encoded/raw duplicate.
|
|
435
|
-
const pageBlocks = Object.keys(loadBlocks()).filter((k) =>
|
|
436
|
-
k.startsWith("pages-"),
|
|
437
|
-
);
|
|
426
|
+
const pageBlocks = Object.keys(loadBlocks()).filter((k) => k.startsWith("pages-"));
|
|
438
427
|
expect(pageBlocks).toHaveLength(1);
|
|
439
428
|
|
|
440
429
|
const match = findPageByPath("/");
|
|
@@ -476,9 +465,7 @@ describe("loadBlocks draft override — key percent-encoding", () => {
|
|
|
476
465
|
}));
|
|
477
466
|
|
|
478
467
|
expect(findPageByPath("/")).toBeNull();
|
|
479
|
-
const pageBlocks = Object.keys(loadBlocks()).filter((k) =>
|
|
480
|
-
k.startsWith("pages-"),
|
|
481
|
-
);
|
|
468
|
+
const pageBlocks = Object.keys(loadBlocks()).filter((k) => k.startsWith("pages-"));
|
|
482
469
|
expect(pageBlocks).toHaveLength(0);
|
|
483
470
|
});
|
|
484
471
|
});
|
|
@@ -534,10 +521,7 @@ describe("loadBlocks draft snapshot semantics", () => {
|
|
|
534
521
|
|
|
535
522
|
// Merge (admin partial payloads): the base-only block survives.
|
|
536
523
|
withBlocksOverride(draft, () => {
|
|
537
|
-
expect(Object.keys(loadBlocks()).sort()).toEqual([
|
|
538
|
-
"pages-home-1",
|
|
539
|
-
"pages-other-2",
|
|
540
|
-
]);
|
|
524
|
+
expect(Object.keys(loadBlocks()).sort()).toEqual(["pages-home-1", "pages-other-2"]);
|
|
541
525
|
});
|
|
542
526
|
});
|
|
543
527
|
|
|
@@ -547,11 +531,27 @@ describe("loadBlocks draft snapshot semantics", () => {
|
|
|
547
531
|
"pages-ambient-9": { name: "A", path: "/a", sections: [{}] },
|
|
548
532
|
}));
|
|
549
533
|
|
|
550
|
-
withDraftBlocks(
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
534
|
+
withDraftBlocks({ "pages-scoped-3": { name: "S", path: "/s", sections: [{}] } }, () => {
|
|
535
|
+
expect(Object.keys(loadBlocks())).toEqual(["pages-scoped-3"]);
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
describe("setBlocks revision", () => {
|
|
541
|
+
it("uses a known revision without re-serializing the decofile", () => {
|
|
542
|
+
const stringify = vi.spyOn(JSON, "stringify");
|
|
543
|
+
setBlocks({ "pages/a.json": { name: "a" } }, "stored-rev");
|
|
544
|
+
expect(getRevision()).toBe("stored-rev");
|
|
545
|
+
expect(stringify).not.toHaveBeenCalled();
|
|
546
|
+
stringify.mockRestore();
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it("computes the same revision as before when none is given", () => {
|
|
550
|
+
const blocks = { "pages/a.json": { name: "a" } };
|
|
551
|
+
setBlocks(blocks);
|
|
552
|
+
const computed = getRevision();
|
|
553
|
+
setBlocks(blocks, computed ?? undefined);
|
|
554
|
+
expect(getRevision()).toBe(computed);
|
|
555
|
+
expect(computed).toBe(computeRevision(blocks));
|
|
556
556
|
});
|
|
557
557
|
});
|
package/src/cms/loader.ts
CHANGED
|
@@ -53,10 +53,7 @@ const blocksOverrideStorage: ALSLike<BlocksOverride> = ALS
|
|
|
53
53
|
// Change listeners
|
|
54
54
|
// ---------------------------------------------------------------------------
|
|
55
55
|
|
|
56
|
-
type ChangeListener = (
|
|
57
|
-
blocks: Record<string, unknown>,
|
|
58
|
-
revision: string,
|
|
59
|
-
) => void;
|
|
56
|
+
type ChangeListener = (blocks: Record<string, unknown>, revision: string) => void;
|
|
60
57
|
const changeListeners: ChangeListener[] = [];
|
|
61
58
|
|
|
62
59
|
/** Register a callback invoked whenever setBlocks() changes the decofile. */
|
|
@@ -84,12 +81,16 @@ function computeRevision(blocks: Record<string, unknown>): string {
|
|
|
84
81
|
* Set the blocks data. Called at startup with generated blocks,
|
|
85
82
|
* and by the admin on hot-reload.
|
|
86
83
|
* Notifies all onChange listeners and updates the revision.
|
|
84
|
+
*
|
|
85
|
+
* Pass `knownRevision` when the source already has it (the KV snapshot stores
|
|
86
|
+
* one, computed with the same `computeRevision`): hashing re-serializes the
|
|
87
|
+
* whole decofile, ~21 MB of garbage on a 10 MB decofile, on every isolate boot.
|
|
87
88
|
*/
|
|
88
|
-
export function setBlocks(blocks: Record<string, unknown
|
|
89
|
+
export function setBlocks(blocks: Record<string, unknown>, knownRevision?: string) {
|
|
89
90
|
// A caller may hand back the same object after mutating it in place.
|
|
90
91
|
pageIndexCache.delete(blocks);
|
|
91
92
|
blockData = blocks;
|
|
92
|
-
revision = computeRevision(blocks);
|
|
93
|
+
revision = knownRevision ?? computeRevision(blocks);
|
|
93
94
|
|
|
94
95
|
// Persist to globalThis so other module instances see them
|
|
95
96
|
G.__deco.blockData = blockData;
|
|
@@ -227,10 +228,7 @@ export function getRevision(): string | null {
|
|
|
227
228
|
* for the duration of the render. Other concurrent requests are not
|
|
228
229
|
* affected (AsyncLocalStorage is per-request scoped).
|
|
229
230
|
*/
|
|
230
|
-
export function withBlocksOverride<T>(
|
|
231
|
-
override: Record<string, unknown>,
|
|
232
|
-
fn: () => T,
|
|
233
|
-
): T {
|
|
231
|
+
export function withBlocksOverride<T>(override: Record<string, unknown>, fn: () => T): T {
|
|
234
232
|
return blocksOverrideStorage.run({ mode: "merge", blocks: override }, fn);
|
|
235
233
|
}
|
|
236
234
|
|
|
@@ -241,10 +239,7 @@ export function withBlocksOverride<T>(
|
|
|
241
239
|
* exactly like the page render, deletions included, or a lazy section could
|
|
242
240
|
* render a block the page no longer has.
|
|
243
241
|
*/
|
|
244
|
-
export function withDraftBlocks<T>(
|
|
245
|
-
draft: Record<string, unknown>,
|
|
246
|
-
fn: () => T,
|
|
247
|
-
): T {
|
|
242
|
+
export function withDraftBlocks<T>(draft: Record<string, unknown>, fn: () => T): T {
|
|
248
243
|
return blocksOverrideStorage.run({ mode: "snapshot", blocks: draft }, fn);
|
|
249
244
|
}
|
|
250
245
|
|
|
@@ -338,8 +333,8 @@ type PageIndexEntry = {
|
|
|
338
333
|
* themselves, so every module instance drops the same entry on `setBlocks()`.
|
|
339
334
|
* The compiled matchers live on the entries and go away with them.
|
|
340
335
|
*/
|
|
341
|
-
const pageIndexCache: WeakMap<Record<string, unknown>, PageIndexEntry[]> =
|
|
342
|
-
|
|
336
|
+
const pageIndexCache: WeakMap<Record<string, unknown>, PageIndexEntry[]> = G.__deco
|
|
337
|
+
.pageIndexCache ?? (G.__deco.pageIndexCache = new WeakMap());
|
|
343
338
|
|
|
344
339
|
function buildPageIndex(blocks: Record<string, unknown>): PageIndexEntry[] {
|
|
345
340
|
const pages: Array<PageIndexEntry & { key2: [number, number, number] }> = [];
|
|
@@ -508,10 +503,7 @@ function assertURLPattern() {
|
|
|
508
503
|
* `URLPattern` is native in browsers, workerd, Deno, and Node >= 24 (this
|
|
509
504
|
* package's `engines` floor). Node 22 and older lack it.
|
|
510
505
|
*/
|
|
511
|
-
export function matchPath(
|
|
512
|
-
pattern: string,
|
|
513
|
-
urlPath: string,
|
|
514
|
-
): Record<string, string> | null {
|
|
506
|
+
export function matchPath(pattern: string, urlPath: string): Record<string, string> | null {
|
|
515
507
|
assertURLPattern();
|
|
516
508
|
return execPattern(cachedPattern(pattern), urlPath);
|
|
517
509
|
}
|