@decocms/blocks 8.11.0 → 8.13.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.
Files changed (67) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/protocol/conformance/blockMetaVectors.json +80 -0
  5. package/dist/protocol/index.d.ts +5 -2
  6. package/dist/protocol/index.d.ts.map +1 -1
  7. package/dist/protocol/index.js +5 -2
  8. package/dist/protocol/index.js.map +1 -1
  9. package/dist/v8/blockMeta.d.ts +33 -4
  10. package/dist/v8/blockMeta.d.ts.map +1 -1
  11. package/dist/v8/blockMeta.js +70 -13
  12. package/dist/v8/blockMeta.js.map +1 -1
  13. package/dist/v8/cli/check/index.d.ts.map +1 -1
  14. package/dist/v8/cli/check/index.js +138 -10
  15. package/dist/v8/cli/check/index.js.map +1 -1
  16. package/dist/v8/client.d.ts +6 -4
  17. package/dist/v8/client.d.ts.map +1 -1
  18. package/dist/v8/client.js +26 -70
  19. package/dist/v8/client.js.map +1 -1
  20. package/dist/v8/cms.d.ts.map +1 -1
  21. package/dist/v8/cms.js +1 -2
  22. package/dist/v8/cms.js.map +1 -1
  23. package/dist/v8/contentIndex.d.ts +18 -7
  24. package/dist/v8/contentIndex.d.ts.map +1 -1
  25. package/dist/v8/contentIndex.js +46 -17
  26. package/dist/v8/contentIndex.js.map +1 -1
  27. package/dist/v8/draftChanges.d.ts.map +1 -1
  28. package/dist/v8/draftChanges.js +4 -1
  29. package/dist/v8/draftChanges.js.map +1 -1
  30. package/dist/v8/index.d.ts +1 -1
  31. package/dist/v8/index.d.ts.map +1 -1
  32. package/dist/v8/remoteLoader.d.ts +4 -2
  33. package/dist/v8/remoteLoader.d.ts.map +1 -1
  34. package/dist/v8/remoteLoader.js +13 -3
  35. package/dist/v8/remoteLoader.js.map +1 -1
  36. package/dist/v8/types.d.ts +47 -7
  37. package/dist/v8/types.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/skills/deco-make-editable/SKILL.md +1 -1
  40. package/skills/deco-make-editable/references/content-shapes.md +14 -9
  41. package/skills/deco-nextjs-site/references/rendering.md +6 -5
  42. package/skills/deco-site-authoring/references/pages.md +6 -4
  43. package/skills/deco-site-workflow/SKILL.md +12 -3
  44. package/skills/deco-site-workflow/references/cli-reference.md +2 -0
  45. package/skills/deco-site-workflow/references/content-layout.md +26 -2
  46. package/skills/deco-storefront-performance/references/request-dedupe.md +5 -3
  47. package/skills/deco-v7-to-v8-migration/SKILL.md +2 -1
  48. package/skills/deco-v7-to-v8-migration/references/hosted-releases.md +1 -1
  49. package/skills/deco-v7-to-v8-migration/references/nextjs.md +2 -2
  50. package/skills/deco-v7-to-v8-migration/references/verify.md +3 -0
  51. package/skills/deco-v7-to-v8-migration/scripts/checkContentReads.ts +32 -0
  52. package/skills/deco-v7-to-v8-migration/scripts/contentReads.ts +157 -0
  53. package/skills/deco-v7-to-v8-migration/scripts/imports.ts +1 -1
  54. package/skills/deco-v7-to-v8-migration/scripts/migrate.ts +4 -0
  55. package/skills/deco-variants/references/multivariate.md +1 -1
  56. package/src/index.ts +1 -1
  57. package/src/protocol/conformance/blockMetaVectors.json +80 -0
  58. package/src/protocol/index.ts +8 -1
  59. package/src/v8/blockMeta.ts +75 -11
  60. package/src/v8/cli/check/index.ts +153 -10
  61. package/src/v8/client.ts +32 -77
  62. package/src/v8/cms.ts +1 -2
  63. package/src/v8/contentIndex.ts +49 -19
  64. package/src/v8/draftChanges.ts +5 -1
  65. package/src/v8/index.ts +1 -1
  66. package/src/v8/remoteLoader.ts +13 -3
  67. package/src/v8/types.ts +48 -7
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Index entries of a hosted release (see /next/hosted-releases-internals):
3
3
  * `revisions/<id>/index.json` and `latest.json` map each block name to its
4
- * hash (`version`) plus the little metadata routing and the reference walk
5
- * need, so neither needs a block body.
4
+ * hash (`version`) plus the little metadata routing, the reference walk and
5
+ * `client.entries()` need, so none of them needs a block body.
6
6
  *
7
- * `blockMeta(body, names)` is the one helper Studio, the SDK and the CLI use
8
- * to compute that metadata, so all three write and read the same entries.
9
- * Pure, synchronous, browser-safe; its only import is the dependency-free
10
- * canonical hash (for `indexEntry`).
7
+ * `blockMeta(body, names, bodyOf?)` is the one helper Studio, the SDK and the
8
+ * CLI use to compute that metadata, so all three write and read the same
9
+ * entries. Pure, synchronous, browser-safe; its only import is the
10
+ * dependency-free canonical hash (for `indexEntry`).
11
11
  *
12
12
  * - `type`: the body's raw top-level `__resolveType`, copied without alias
13
13
  * mapping (readers map it with their alias rules).
@@ -15,12 +15,32 @@
15
15
  * - `refs`: every `__resolveType` string anywhere in the body (the top-level
16
16
  * one included, every variant, `never` and `lazy` values included) that is
17
17
  * one of `names`, deduped and sorted in code-unit order.
18
+ * - `noIndex`: `true` when the body is a page that asks search engines not to
19
+ * index it: it has a top-level string `path` and its top-level `seo` says
20
+ * `noIndexing: true`, inline or through the saved SEO block it references
21
+ * (`{ "__resolveType": "DefaultSeo" }`), which `bodyOf` looks up (see
22
+ * `seoNoIndex`). Written only when `true`.
18
23
  *
19
- * A key is present only when it has a value (`refs: []` is left out), so a
20
- * body with nothing to say gives `{}` and its entry carries no `meta`.
24
+ * A key is present only when it has a value (`refs: []` and `noIndex: false`
25
+ * are left out), so a body with nothing to say gives `{}` and its entry
26
+ * carries no `meta`.
27
+ *
28
+ * `META_VERSION` is the version of this metadata. An index says which version
29
+ * computed its entries (its top-level `metaVersion`; absent is 1). A reader
30
+ * trusts a missing `noIndex` as `false` only on an index at version 2 or
31
+ * later, and a publish that finds an older index recomputes every entry.
21
32
  */
22
33
  import { blockVersion } from "./canonical.ts";
23
34
 
35
+ /**
36
+ * The version of the metadata `blockMeta` computes: 1 is `type`, `path` and
37
+ * `refs`; 2 adds `noIndex`. An index without `metaVersion` is version 1.
38
+ */
39
+ export const META_VERSION = 2;
40
+
41
+ /** A saved block's body by name, to follow a reference (`undefined`: not known). */
42
+ export type BodyOf = (name: string) => unknown;
43
+
24
44
  /** What an index says about a block, besides its hash. */
25
45
  export interface BlockMeta {
26
46
  /** The body's raw `__resolveType`. */
@@ -29,6 +49,8 @@ export interface BlockMeta {
29
49
  path?: string;
30
50
  /** The block names the body references, deduped, in code-unit order. */
31
51
  refs?: string[];
52
+ /** `true` when the page says `noIndexing` (meta version 2); never written `false`. */
53
+ noIndex?: boolean;
32
54
  }
33
55
 
34
56
  /** One entry of a revision index. */
@@ -47,10 +69,18 @@ export type BlockIndex = Record<string, IndexEntry>;
47
69
  /** The well-known name whose body every index inlines as `data`. */
48
70
  export const INLINE_BLOCK = "CMS";
49
71
 
50
- /** The metadata of one block body; `names` are the block names of the index being built. */
72
+ /** How many saved blocks `seoNoIndex` follows, one referencing the next. */
73
+ const MAX_SEO_HOPS = 8;
74
+
75
+ /**
76
+ * The metadata of one block body. `names` are the block names of the index
77
+ * being built; `bodyOf` gives the bodies of the saved blocks a page's `seo`
78
+ * may reference (without it, only an inline `seo` counts for `noIndex`).
79
+ */
51
80
  export function blockMeta(
52
81
  body: unknown,
53
82
  names: ReadonlySet<string> | readonly string[],
83
+ bodyOf?: BodyOf,
54
84
  ): BlockMeta {
55
85
  const meta: BlockMeta = {};
56
86
  if (!isRecord(body)) return meta;
@@ -60,17 +90,51 @@ export function blockMeta(
60
90
  const found = new Set<string>();
61
91
  collectRefs(body, known, found);
62
92
  if (found.size > 0) meta.refs = [...found].sort();
93
+ if (pageNoIndex(body, bodyOf)) meta.noIndex = true;
63
94
  return meta;
64
95
  }
65
96
 
66
- /** The index entry for a block: its hash, its metadata when any, and its body when it is `CMS`. */
97
+ /**
98
+ * Whether a page body asks not to be indexed: it has a top-level string
99
+ * `path`, and `seoNoIndex(body.seo, bodyOf)`.
100
+ */
101
+ export function pageNoIndex(body: unknown, bodyOf?: BodyOf): boolean {
102
+ return isRecord(body) && typeof body.path === "string" && seoNoIndex(body.seo, bodyOf);
103
+ }
104
+
105
+ /**
106
+ * Whether a page's `seo` value says `noIndexing: true`: its own boolean
107
+ * `noIndexing` when it has one; otherwise, when its `__resolveType` is a saved
108
+ * block `bodyOf` knows (a saved SEO block such as `DefaultSeo`), that block's
109
+ * answer, following up to 8 saved blocks. Anything else (no `seo`, a variant,
110
+ * a block `bodyOf` doesn't know, a cycle) is `false`.
111
+ */
112
+ export function seoNoIndex(seo: unknown, bodyOf?: BodyOf): boolean {
113
+ let node = seo;
114
+ const seen = new Set<string>();
115
+ for (let hop = 0; hop <= MAX_SEO_HOPS; hop++) {
116
+ if (!isRecord(node)) return false;
117
+ if (typeof node.noIndexing === "boolean") return node.noIndexing;
118
+ const ref = node.__resolveType;
119
+ if (bodyOf === undefined || typeof ref !== "string" || seen.has(ref)) return false;
120
+ seen.add(ref);
121
+ node = bodyOf(ref);
122
+ }
123
+ return false;
124
+ }
125
+
126
+ /**
127
+ * The index entry for a block: its hash, its metadata when any, and its body
128
+ * when it is `CMS`. `bodyOf` as for `blockMeta`.
129
+ */
67
130
  export async function indexEntry(
68
131
  name: string,
69
132
  body: unknown,
70
133
  names: ReadonlySet<string> | readonly string[],
134
+ bodyOf?: BodyOf,
71
135
  ): Promise<IndexEntry> {
72
136
  const entry: IndexEntry = { version: await blockVersion(body) };
73
- const meta = blockMeta(body, names);
137
+ const meta = blockMeta(body, names, bodyOf);
74
138
  if (Object.keys(meta).length > 0) entry.meta = meta;
75
139
  if (name === INLINE_BLOCK) entry.data = body;
76
140
  return entry;
@@ -459,6 +459,129 @@ class Checker {
459
459
  return this.underlyingType(entry.__resolveType, [...chain, name]);
460
460
  }
461
461
 
462
+ /**
463
+ * The saved blocks a reference to `name` walks, first to last (`last` is
464
+ * typed by a block type: `{ __resolveType: "site/sections/Footer.tsx" }`),
465
+ * or null when the walk loops or ends at a name that is nothing.
466
+ */
467
+ private referenceChain(name: string): string[] | null {
468
+ const chain: string[] = [];
469
+ let cur = name;
470
+ while (this.canonicalType(cur) === null) {
471
+ const entry = own(this.saved.blocks, cur);
472
+ if (chain.includes(cur) || !entry || typeof entry.__resolveType !== "string") return null;
473
+ chain.push(cur);
474
+ cur = entry.__resolveType;
475
+ }
476
+ return chain.length > 0 ? chain : null;
477
+ }
478
+
479
+ /**
480
+ * The props a reference to `chain[0]` resolves with, the way the resolver
481
+ * merges them (mergeReference, hop by hop): the last block's props, each
482
+ * nearer block's over them, `overrides` on top, and the last block's type.
483
+ */
484
+ private mergedChain(chain: string[], overrides: Record<string, Json>): Record<string, Json> {
485
+ const merged: Record<string, Json> = {};
486
+ for (const name of [...chain].reverse()) Object.assign(merged, this.saved.blocks[name]);
487
+ Object.assign(merged, overrides);
488
+ merged.__resolveType = this.saved.blocks[chain[chain.length - 1]].__resolveType;
489
+ return merged;
490
+ }
491
+
492
+ /**
493
+ * A saved block typed as another saved block (`Footer-Island` =
494
+ * `{ "__resolveType": "Footer", "revealOnScroll": true }`) renders: the
495
+ * resolver follows the chain and merges each hop's props. Studio's editor
496
+ * doesn't: it follows one hop, looks up the form of the type it finds there
497
+ * (`Footer`, a saved block's name, has none) and shows an empty form, so
498
+ * the block can't be edited. A bare alias (`{ "__resolveType": "Footer" }`)
499
+ * is the same. Reported on the block itself, once, not at each reference
500
+ * to it: retyping the block fixes them all.
501
+ *
502
+ * A *reference* (a bare `{ "__resolveType": "Footer" }` in a page's
503
+ * sections, a variant, a lazy block or a prop) is fine: Studio follows its
504
+ * one hop to `Footer`, typed by its section, and edits that. See
505
+ * referenceOverrides for a reference that carries props of its own.
506
+ */
507
+ private savedBlockChain(name: string, entry: Record<string, Json>): string | null {
508
+ const chain = this.referenceChain(entry.__resolveType);
509
+ if (chain === null) return null; // a cycle or a missing name: checkBlock reports it
510
+ const parent = chain[0];
511
+ const type = String(this.saved.blocks[chain[chain.length - 1]].__resolveType);
512
+ const inherited = this.inheritedProps(chain, entry);
513
+ const copy = inherited
514
+ ? ` and copy in the props it inherits from "${parent}" (${inherited})`
515
+ : "";
516
+ return (
517
+ `saved block "${name}" is typed as another saved block (${[name, ...chain, type].join(" → ")}): ` +
518
+ `it renders, but Studio's editor follows one hop, finds no form for "${parent}" and shows an empty one. ` +
519
+ `Set its __resolveType to "${type}"${copy}`
520
+ );
521
+ }
522
+
523
+ /**
524
+ * The props a block that sets `own` gets from the chain it is typed by
525
+ * (`chain[0]` onward), listed for a message: up to 12, then "and N more".
526
+ */
527
+ private inheritedProps(chain: string[], own: Record<string, Json>): string {
528
+ const keys = Object.keys(this.mergedChain(chain, {})).filter(
529
+ (key) => key !== "__resolveType" && !Object.hasOwn(own, key),
530
+ );
531
+ const more = keys.length > 12 ? ` and ${keys.length - 12} more` : "";
532
+ return keys.slice(0, 12).join(", ") + more;
533
+ }
534
+
535
+ /**
536
+ * A reference that carries props of its own (a page section
537
+ * `{ "__resolveType": "Footer", "hasFixedNewsletter": true }`) renders with
538
+ * them merged over the saved block's (shallowly: `"seo": {}` replaces the
539
+ * saved `seo`). Studio's editor shows and edits the saved block itself: it
540
+ * ignores these props, saves every edit to the shared block (changing every
541
+ * page that uses it) and drops them on detach. Only a bare reference, or
542
+ * the block inlined (typed by its section, with the props copied in), is
543
+ * editable there.
544
+ */
545
+ private referenceOverrides(block: Record<string, Json>, overrides: string[]): string {
546
+ const name: string = block.__resolveType;
547
+ const chain = this.referenceChain(name);
548
+ const type = chain ? String(this.saved.blocks[chain[chain.length - 1]].__resolveType) : null;
549
+ const inherited = chain ? this.inheritedProps(chain, block) : "";
550
+ const copy = inherited
551
+ ? ` and copy in the props of "${name}" it doesn't set (${inherited})`
552
+ : "";
553
+ const instead = type ? `, or inline it: set its __resolveType to "${type}"${copy}` : "";
554
+ return (
555
+ `reference to saved block "${name}" carries its own props (${overrides.join(", ")}): ` +
556
+ `they apply when the page renders, but Studio's editor shows and edits "${name}" itself, ` +
557
+ `ignores them and saves edits to "${name}" for every use. ` +
558
+ `Make it a bare reference ({ "__resolveType": "${name}" })${instead}`
559
+ );
560
+ }
561
+
562
+ /**
563
+ * Why Studio's editor can't open a reference to the saved block `name`, if
564
+ * it can't: Studio (isSavedBlockResolveType) reads a name ending in "." and
565
+ * letters or digits ("Promo 2.0") as a module path, and never treats
566
+ * "__proto__" or "constructor" as a saved block. It then finds no form for
567
+ * the name and shows an empty one; the page still renders.
568
+ */
569
+ private unopenableName(name: string): string | null {
570
+ const ending = /\.\w+$/.exec(name)?.[0];
571
+ const reason = ending
572
+ ? `its name ends in "${ending}", so Studio's editor reads it as a module path`
573
+ : name === "__proto__" || name === "constructor"
574
+ ? `Studio's editor never reads "${name}" as a saved block's name`
575
+ : null;
576
+ if (reason === null) return null;
577
+ return (
578
+ `reference to saved block "${name}" can't be edited in Studio: ${reason}, ` +
579
+ `finds no form for it and shows an empty one. Rename the saved block` +
580
+ (ending ? ` (no "." followed by letters or digits at the end)` : "") +
581
+ ` and update its references`
582
+ );
583
+ }
584
+
462
585
  /**
463
586
  * Whether `name` is the built-in itself here: the block map didn't define
464
587
  * the same key (which replaces the built-in, schema included).
@@ -667,8 +790,17 @@ class Checker {
667
790
  }
668
791
  }
669
792
 
670
- /** Check a block that sits in `field`, dispatching on its `__resolveType`. */
671
- checkBlock(ctx: Context, block: Record<string, Json>, path: string, field: Json | null) {
793
+ /**
794
+ * Check a block that sits in `field`, dispatching on its `__resolveType`.
795
+ * `top`: it is a saved block itself, not a value in one.
796
+ */
797
+ checkBlock(
798
+ ctx: Context,
799
+ block: Record<string, Json>,
800
+ path: string,
801
+ field: Json | null,
802
+ top = false,
803
+ ) {
672
804
  const name: string = block.__resolveType;
673
805
  const type = this.canonicalType(name);
674
806
 
@@ -692,6 +824,8 @@ class Checker {
692
824
  return;
693
825
  }
694
826
  if (underlying === null) return; // the saved block reports its own problem
827
+ const unopenable = top ? null : this.unopenableName(name);
828
+ if (unopenable) this.report(ctx, path, unopenable);
695
829
  const fit = this.fitProblem(underlying, field);
696
830
  if (fit) {
697
831
  this.report(
@@ -701,14 +835,16 @@ class Checker {
701
835
  );
702
836
  return;
703
837
  }
704
- // Overrides are merged over the saved arguments for this use.
838
+ // Overrides are merged over the saved arguments for this use, along the
839
+ // whole chain of saved blocks (the resolver's mergeReference, per hop).
705
840
  const overrides = Object.keys(block).filter((k) => k !== "__resolveType");
706
- if (overrides.length > 0) {
707
- const target = this.saved.blocks[name];
708
- const targetType = String(target.__resolveType);
709
- const merged = { ...target, ...block, __resolveType: targetType };
710
- if (this.isCalendar(targetType)) this.checkCalendar(ctx, merged, path);
711
- else if (this.canonicalType(targetType)) this.checkProps(ctx, merged, path, true);
841
+ const chain = this.referenceChain(name);
842
+ if (overrides.length > 0 && chain) {
843
+ // A saved block typed this way is savedBlockChain's (run reports it).
844
+ if (!top) this.report(ctx, path, this.referenceOverrides(block, overrides));
845
+ const merged = this.mergedChain(chain, block);
846
+ if (this.isCalendar(merged.__resolveType)) this.checkCalendar(ctx, merged, path);
847
+ else this.checkProps(ctx, merged, path, true);
712
848
  }
713
849
  return;
714
850
  }
@@ -940,7 +1076,14 @@ class Checker {
940
1076
  if (V7_REDIRECT_TYPES.has(entry.__resolveType) && !this.canonicalType(entry.__resolveType)) {
941
1077
  continue;
942
1078
  }
943
- this.checkBlock(ctx, entry, "", null);
1079
+ if (
1080
+ this.canonicalType(entry.__resolveType) === null &&
1081
+ Object.hasOwn(this.saved.blocks, entry.__resolveType)
1082
+ ) {
1083
+ const chained = this.savedBlockChain(name, entry);
1084
+ if (chained) this.report(ctx, "", chained);
1085
+ }
1086
+ this.checkBlock(ctx, entry, "", null, true);
944
1087
  }
945
1088
  problems.push(...this.builtInOverrides());
946
1089
  problems.push(...this.routeProblems());
package/src/v8/client.ts CHANGED
@@ -8,18 +8,19 @@
8
8
  * The binding flow (see /next/routing): `routes()` → `matchRoute` →
9
9
  * `prefetch(name)` → `redirects()` → `matchRoute` → `resolve(name)`. The
10
10
  * page's subtree and the redirects block go out in one parallel wave.
11
+ * `entries(type?)` answers from the index too: what a sitemap or a lookup by
12
+ * path needs, with no body fetched.
11
13
  */
12
14
  import { keepAlive } from "./background.ts";
13
15
  import {
14
- canonicalType,
15
- namesOfType,
16
- REDIRECTS_TYPE,
16
+ entriesOf,
17
+ type IndexedContent,
17
18
  redirectsBlockOf,
18
19
  routesOf,
19
20
  walk,
20
21
  } from "./contentIndex.ts";
21
22
  import { CAMPAIGN_OVERRIDES, EXPERIMENT_OVERRIDES, type Overrides } from "./current.ts";
22
- import { errors, isResolutionError, ResolutionError } from "./errors.ts";
23
+ import { errors, isResolutionError } from "./errors.ts";
23
24
  import type { Visitor } from "./experiments.ts";
24
25
  import { isPlainObject } from "./json.ts";
25
26
  import { choosePin, type Pin, rootsOf, type Selection } from "./pin.ts";
@@ -30,6 +31,7 @@ import type {
30
31
  Blocks,
31
32
  Client,
32
33
  CMSError,
34
+ Entry,
33
35
  LegacyRedirect,
34
36
  ListOptions,
35
37
  Redirect,
@@ -58,13 +60,16 @@ interface ClientOptions {
58
60
  telemetry?: ClientTelemetry;
59
61
  }
60
62
 
63
+ /** What `client.list()` throws: it was removed, and what to read instead. */
64
+ export const LIST_REMOVED =
65
+ "[decocms/blocks] client.list() was removed in this version of @decocms/blocks: it fetched the " +
66
+ "body of every block of a type. Route with client.routes() and client.redirects() (index only); " +
67
+ "read a type's names, paths, refs and noIndex with client.entries(type) (index only); read a " +
68
+ "block's body with client.resolve(name).";
69
+
61
70
  type Selected = { selection: Selection } | { error: CMSError };
62
71
  type Pinned = { pin: Pin; resolver: Resolver } | { error: CMSError };
63
72
 
64
- /** Types `list()` warns about once per process: routing reads `routes()` and `redirects()`. */
65
- const ROUTED_TYPES = new Set(["page", "redirect", REDIRECTS_TYPE]);
66
- const LIST_WARNED = Symbol.for("decocms.blocks.listWarned");
67
-
68
73
  export class CMSClient implements Client {
69
74
  readonly #options: ClientOptions;
70
75
  readonly #reported = new WeakSet<CMSError>();
@@ -102,15 +107,29 @@ export class CMSClient implements Client {
102
107
  }
103
108
 
104
109
  async routes(): Promise<Result<Route[]>> {
105
- // A pinned client answers from its pin; otherwise the current index, without pinning.
110
+ const [content, error] = await this.#index();
111
+ return error === null ? [routesOf(content), null] : this.#fail(error);
112
+ }
113
+
114
+ async entries(type?: string): Promise<Result<readonly Entry[]>> {
115
+ if (type !== undefined && typeof type !== "string") return [[], null];
116
+ const [content, error] = await this.#index();
117
+ return error === null ? [entriesOf(content, type), null] : this.#fail(error);
118
+ }
119
+
120
+ /** Removed: throws {@link LIST_REMOVED} before reading anything. */
121
+ list<T = Block>(_type: string, _options?: ListOptions<T>): Promise<Result<T[]>> {
122
+ throw new Error(LIST_REMOVED);
123
+ }
124
+
125
+ /** The index it reads: a pinned client's pin; otherwise the current one, without pinning. */
126
+ async #index(): Promise<[IndexedContent, null] | [null, CMSError]> {
106
127
  const pinned = this.#pinned === undefined ? undefined : await this.#pinned;
107
128
  if (pinned !== undefined) {
108
- if ("error" in pinned) return this.#fail(pinned.error);
109
- return [routesOf(pinned.pin.content), null];
129
+ return "error" in pinned ? [null, pinned.error] : [pinned.pin.content, null];
110
130
  }
111
131
  const selected = await this.#select();
112
- if ("error" in selected) return this.#fail(selected.error);
113
- return [routesOf(selected.selection.current), null];
132
+ return "error" in selected ? [null, selected.error] : [selected.selection.current, null];
114
133
  }
115
134
 
116
135
  prefetch(name: string): void {
@@ -163,38 +182,6 @@ export class CMSClient implements Client {
163
182
  }
164
183
  }
165
184
 
166
- async list<T = Block>(type: string, options: ListOptions<T> = {}): Promise<Result<T[]>> {
167
- const pinned = await this.#pin();
168
- if ("error" in pinned) return this.#fail(pinned.error);
169
- const { pin, resolver } = pinned;
170
- warnRoutedList(canonicalType(pin.content, type));
171
- const names = namesOfType(pin.content, type);
172
-
173
- let entries: T[];
174
- try {
175
- await pin.wave(walk(pin.content, names));
176
- const values = await Promise.all(
177
- names.map((name) =>
178
- (options.run === true ? resolver.resolveEntry(name) : resolver.expandEntry(name)).catch(
179
- (error) => {
180
- throw atEntry(name, error);
181
- },
182
- ),
183
- ),
184
- );
185
- entries = values.filter((value) => value !== undefined) as T[];
186
- } catch (error) {
187
- return this.#fail(asCMSError(error));
188
- }
189
-
190
- if (options.where) entries = entries.filter(options.where);
191
- if (options.sort) entries = [...entries].sort(options.sort);
192
- if (typeof options.limit === "number" && options.limit >= 0) {
193
- entries = entries.slice(0, Math.floor(options.limit));
194
- }
195
- return [entries, null];
196
- }
197
-
198
185
  async revision(): Promise<string> {
199
186
  const pinned = await this.#pin();
200
187
  if ("error" in pinned) throw pinned.error;
@@ -256,32 +243,6 @@ function rulesOf(value: unknown): Redirect[] {
256
243
  ) as Redirect[];
257
244
  }
258
245
 
259
- /**
260
- * `list("page")` and `list("redirect")` read every page or rule: routing reads
261
- * `routes()` and `redirects()` from the index instead. Warns once per process
262
- * and type, outside production.
263
- */
264
- function warnRoutedList(type: string): void {
265
- if (!ROUTED_TYPES.has(type)) return;
266
- const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process
267
- ?.env;
268
- if (env?.NODE_ENV === "production") return;
269
- const store = globalThis as unknown as Record<symbol, Set<string> | undefined>;
270
- const warned = store[LIST_WARNED] ?? new Set<string>();
271
- store[LIST_WARNED] = warned;
272
- if (warned.has(type)) return;
273
- warned.add(type);
274
- console.warn(
275
- `[decocms/blocks] client.list("${type}") fetches every ${type} block; route with ` +
276
- "client.routes() and client.redirects(), which read the index (see /next/routing).",
277
- );
278
- }
279
-
280
- /** Forgets which list types warned, so a test starts clean. */
281
- export function resetListWarnings(): void {
282
- delete (globalThis as unknown as Record<symbol, unknown>)[LIST_WARNED];
283
- }
284
-
285
246
  /**
286
247
  * The client's own frozen copy of a visitor, so a caller changing its object
287
248
  * later changes no client; `null` for anything that isn't one.
@@ -307,9 +268,3 @@ function copyVisitor(visitor: Visitor | null | undefined): Visitor | null {
307
268
  function asCMSError(error: unknown): CMSError {
308
269
  return isResolutionError(error) ? error : errors.blockFailed("<unknown>", [], error);
309
270
  }
310
-
311
- /** Prefixes a failed list entry's path with the entry's name, so the log says which one. */
312
- function atEntry(name: string, error: unknown): unknown {
313
- if (!isResolutionError(error) || error.code === "CYCLE") return error;
314
- return new ResolutionError(error.code, error.message, [name, ...error.path], error.cause);
315
- }
package/src/v8/cms.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  import { runInBackground } from "./background.ts";
10
10
  import { SETTINGS_BLOCK, SETTINGS_TYPE } from "./builtins/data.ts";
11
11
  import { builtIns } from "./builtins/index.ts";
12
- import { CMSClient, resetListWarnings } from "./client.ts";
12
+ import { CMSClient } from "./client.ts";
13
13
  import { ContentStore, isLoader, isSnapshot } from "./content.ts";
14
14
  import type { IndexedContent } from "./contentIndex.ts";
15
15
  import type { Overrides } from "./current.ts";
@@ -485,7 +485,6 @@ function overridesOf(given: unknown): Overrides | undefined {
485
485
  export function resetForTests(): void {
486
486
  clearGlobals(INSTANCE_PREFIX);
487
487
  resetRemoteLoaders();
488
- resetListWarnings();
489
488
  setCurrentTelemetry(undefined);
490
489
  }
491
490
 
@@ -6,14 +6,15 @@
6
6
  * the inline `CMS` body) or, by hash, from the hosted block store.
7
7
  *
8
8
  * Local content (a content module or a custom `Loader`) is turned into an
9
- * index once per snapshot object, with `blockMeta` over its bodies, so
10
- * `routes()`, `redirects()` and `prefetch()` behave the same everywhere.
9
+ * index once per snapshot object, with `blockMeta` over its bodies (a page's
10
+ * saved SEO block resolved from them), so `routes()`, `redirects()`,
11
+ * `entries()` and `prefetch()` behave the same everywhere.
11
12
  */
12
- import { type BlockIndex, blockMeta, type IndexEntry } from "./blockMeta.ts";
13
+ import { type BlockIndex, blockMeta, type IndexEntry, META_VERSION } from "./blockMeta.ts";
13
14
  import { REDIRECTS_TYPE } from "./builtins/data.ts";
14
15
  import { LEGACY_ALIASES } from "./builtins/legacy.ts";
15
16
  import { own } from "./json.ts";
16
- import type { Route, Snapshot } from "./types.ts";
17
+ import type { Entry, Route, Snapshot } from "./types.ts";
17
18
 
18
19
  /** Fetches bodies by hash, shared across requests (see ./blockStore.ts). */
19
20
  export interface BodySource {
@@ -34,6 +35,11 @@ export interface IndexedContent {
34
35
  readonly revision: string;
35
36
  /** `latest.json`'s `sequence` (0 for local content). */
36
37
  readonly sequence: number;
38
+ /**
39
+ * The version of `blockMeta` that computed the entries' `meta` (the index's
40
+ * `metaVersion`, 1 when it has none; `META_VERSION` for local content).
41
+ */
42
+ readonly metaVersion: number;
37
43
  /** Name → entry; a null-prototype object, so any name is an entry. */
38
44
  readonly blocks: Readonly<BlockIndex>;
39
45
  /** The content module's alias table (local content only). */
@@ -84,6 +90,8 @@ interface Derived {
84
90
  names: string[];
85
91
  routes: Route[];
86
92
  redirects: string | null;
93
+ /** `entries(type)` by canonical type (`""`: every typed entry), built on first ask. */
94
+ entries: Map<string, readonly Entry[]>;
87
95
  }
88
96
 
89
97
  const derived = new WeakMap<IndexedContent, Derived>();
@@ -102,16 +110,11 @@ function derive(content: IndexedContent): Derived {
102
110
  if (type === PAGE && typeof meta.path === "string") routes.push({ name, path: meta.path });
103
111
  else if (type === REDIRECTS_TYPE && redirects === null) redirects = name;
104
112
  }
105
- found = { names, routes, redirects };
113
+ found = { names, routes, redirects, entries: new Map() };
106
114
  derived.set(content, found);
107
115
  return found;
108
116
  }
109
117
 
110
- /** Every name, in code-unit order. */
111
- export function namesOf(content: IndexedContent): readonly string[] {
112
- return derive(content).names;
113
- }
114
-
115
118
  /**
116
119
  * `{ name, path }` of every page entry (its `meta.type` maps to `page` and it
117
120
  * has a `meta.path`), in code-unit order of name, so the same page wins a path
@@ -127,13 +130,38 @@ export function redirectsBlockOf(content: IndexedContent): string | null {
127
130
  return derive(content).redirects;
128
131
  }
129
132
 
130
- /** Names of entries whose type maps to `type`, in code-unit order. */
131
- export function namesOfType(content: IndexedContent, type: string): string[] {
132
- const wanted = canonicalType(content, type);
133
- return namesOf(content).filter((name) => {
134
- const t = content.blocks[name]?.meta?.type;
135
- return t !== undefined && canonicalType(content, t) === wanted;
136
- });
133
+ /**
134
+ * What the index says about each typed entry (of `type`, after alias mapping,
135
+ * when given), in code-unit order of name: `{ name, type, path?, refs,
136
+ * noIndex? }`, `type` mapped through the aliases. `noIndex` is a boolean on an
137
+ * index whose meta version has it (`metaVersion` 2 or later: a missing one is
138
+ * `false`), absent on an older one. Never reads a body. The same frozen array
139
+ * for the same index and type.
140
+ */
141
+ export function entriesOf(content: IndexedContent, type?: string): readonly Entry[] {
142
+ const derived = derive(content);
143
+ const wanted = type === undefined ? "" : canonicalType(content, type);
144
+ let found = derived.entries.get(wanted);
145
+ if (found !== undefined) return found;
146
+ const knowsNoIndex = content.metaVersion >= 2;
147
+ const out: Entry[] = [];
148
+ for (const name of derived.names) {
149
+ const meta = content.blocks[name]?.meta;
150
+ if (meta?.type === undefined) continue;
151
+ const canonical = canonicalType(content, meta.type);
152
+ if (wanted !== "" && canonical !== wanted) continue;
153
+ const entry: Entry = {
154
+ name,
155
+ type: canonical,
156
+ refs: Object.freeze([...(meta.refs ?? [])]) as string[],
157
+ };
158
+ if (meta.path !== undefined) entry.path = meta.path;
159
+ if (knowsNoIndex) entry.noIndex = meta.noIndex === true;
160
+ out.push(Object.freeze(entry));
161
+ }
162
+ found = Object.freeze(out);
163
+ derived.entries.set(wanted, found);
164
+ return found;
137
165
  }
138
166
 
139
167
  /**
@@ -162,19 +190,21 @@ export function localIndex(snapshot: Snapshot): IndexedContent {
162
190
  if (found !== undefined) return found;
163
191
  const bodies = snapshot.blocks;
164
192
  const names = new Set(Object.keys(bodies));
193
+ const bodyOf = (name: string) => (Object.hasOwn(bodies, name) ? bodies[name] : undefined);
165
194
  const blocks = Object.create(null) as BlockIndex;
166
195
  for (const name of names) {
167
- const meta = blockMeta(bodies[name], names);
196
+ const meta = blockMeta(bodies[name], names, bodyOf);
168
197
  // Local bodies are held, so no hash is needed (or computed).
169
198
  blocks[name] = Object.keys(meta).length > 0 ? { version: "", meta } : { version: "" };
170
199
  }
171
200
  found = {
172
201
  revision: snapshot.revision,
173
202
  sequence: 0,
203
+ metaVersion: META_VERSION,
174
204
  blocks,
175
205
  held: {
176
206
  has: (name) => Object.hasOwn(bodies, name),
177
- get: (name) => (Object.hasOwn(bodies, name) ? bodies[name] : undefined),
207
+ get: bodyOf,
178
208
  },
179
209
  };
180
210
  if (snapshot.aliases !== undefined) (found as { aliases?: unknown }).aliases = snapshot.aliases;
@@ -245,9 +245,12 @@ export function overlayDraft(
245
245
  }
246
246
  }
247
247
  const blocks = Object.create(null) as BlockIndex;
248
+ // A page's saved SEO block: the draft's own body, else one the base holds.
249
+ const bodyOf = (name: string): unknown =>
250
+ bodies.has(name) ? bodies.get(name) : states.has(name) ? undefined : base.held?.get(name);
248
251
  for (const name of names) {
249
252
  if (bodies.has(name)) {
250
- const meta = blockMeta(bodies.get(name), names);
253
+ const meta = blockMeta(bodies.get(name), names, bodyOf);
251
254
  const entry: IndexEntry = { version: "" };
252
255
  if (Object.keys(meta).length > 0) entry.meta = meta;
253
256
  blocks[name] = entry;
@@ -264,6 +267,7 @@ export function overlayDraft(
264
267
  const content: IndexedContent = {
265
268
  revision: `${base.revision}~${view}`,
266
269
  sequence: base.sequence,
270
+ metaVersion: base.metaVersion,
267
271
  blocks,
268
272
  held,
269
273
  draft: true,