@backmatter/core 0.0.43 → 0.0.44

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": "@backmatter/core",
3
- "version": "0.0.43",
3
+ "version": "0.0.44",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/config.ts CHANGED
@@ -28,6 +28,18 @@ export type BackmatterDirs = {
28
28
  sources?: string;
29
29
  };
30
30
 
31
+ /**
32
+ * How long a published document stays cached between commits. A Next
33
+ * `cacheLife` profile name ("hours", "days", "max", or one the host defines)
34
+ * or custom seconds. Commits already refresh what they touch, so this only
35
+ * bounds how stale a page can get when a commit notification is lost.
36
+ */
37
+ export type CacheLifeProfile =
38
+ | string
39
+ | { stale?: number; revalidate?: number; expire?: number };
40
+
41
+ export const DEFAULT_CACHE_LIFE = "days";
42
+
31
43
  export type BackmatterConfig = {
32
44
  /** Workspace id (`ws_…`), not the dashboard slug. Config first; env BACKMATTER_WORKSPACE_ID overrides gap. */
33
45
  workspaceId?: string;
@@ -50,6 +62,8 @@ export type BackmatterConfig = {
50
62
  siteRoot?: string;
51
63
  dirs?: BackmatterDirs;
52
64
  devBar?: DevBarHostConfig;
65
+ /** Cache lifetime of published content on a Next host. Default: "days". */
66
+ cacheLife?: CacheLifeProfile;
53
67
  };
54
68
 
55
69
  export type ResolvedConfig = Omit<BackmatterConfig, "dirs"> & {
@@ -207,6 +221,7 @@ export function resolveConfig(
207
221
  siteUrl: detectSiteUrl(config, env),
208
222
  siteRoot: config.siteRoot,
209
223
  devBar: config.devBar,
224
+ cacheLife: config.cacheLife,
210
225
  dirs: {
211
226
  models: config.dirs?.models ?? DEFAULT_DIRS.models,
212
227
  blocks: config.dirs?.blocks ?? DEFAULT_DIRS.blocks,
@@ -218,3 +233,8 @@ export function resolveConfig(
218
233
  },
219
234
  };
220
235
  }
236
+
237
+ /** The `cacheLife` profile for published content. */
238
+ export function cacheLifeProfile(config: Pick<BackmatterConfig, "cacheLife">): CacheLifeProfile {
239
+ return config.cacheLife ?? DEFAULT_CACHE_LIFE;
240
+ }
@@ -28,28 +28,51 @@ export function revalidateUrl(siteUrl: string): string {
28
28
  return `${siteUrl.replace(/\/$/, "")}${BACKMATTER_REVALIDATE_PATH}`;
29
29
  }
30
30
 
31
+ /**
32
+ * Tags on one document's cache entry. No type tag: a document page must not
33
+ * expire because a sibling of the same type changed. A page that also lists
34
+ * documents picks up the type tag from the nested list entry.
35
+ */
31
36
  export function contentCacheTags(type: string, slug: string): string[] {
32
- return [BACKMATTER_CACHE_TAG, `backmatter:${type}`, `backmatter:${type}:${slug}`];
37
+ return [BACKMATTER_CACHE_TAG, docCacheTag(type, slug)];
33
38
  }
34
39
 
40
+ /** Tags on a list entry. The type tag is what a commit to that type refreshes. */
35
41
  export function listCacheTags(type?: string): string[] {
36
- return type ? [BACKMATTER_CACHE_TAG, `backmatter:${type}`] : [BACKMATTER_CACHE_TAG];
42
+ return type ? [BACKMATTER_CACHE_TAG, typeCacheTag(type)] : [BACKMATTER_CACHE_TAG];
43
+ }
44
+
45
+ function typeCacheTag(type: string): string {
46
+ return `backmatter:${type}`;
37
47
  }
38
48
 
49
+ function docCacheTag(type: string, slug: string): string {
50
+ return `backmatter:${type}:${slug}`;
51
+ }
52
+
53
+ export type CommitCacheTags = {
54
+ /** Expire now: the next visitor waits for fresh content. */
55
+ expire: string[];
56
+ /** Mark stale: visitors get the old entry while it re-renders. */
57
+ refresh: string[];
58
+ };
59
+
39
60
  /**
40
- * Cache tags a commit should expire. A normal commit expires the touched
41
- * documents and their type lists. A full rebuild expires the global tag,
61
+ * Cache tags a commit touches. The touched documents expire now. Their type
62
+ * lists refresh in the background. A full rebuild expires the global tag,
42
63
  * which every cached entry carries.
43
64
  */
44
- export function revalidateTagsForCommit(event: Pick<ContentCommittedEvent, "docs" | "full">): string[] {
45
- if (event.full) return [BACKMATTER_CACHE_TAG];
46
- const tags = new Set<string>();
65
+ export function revalidateTagsForCommit(
66
+ event: Pick<ContentCommittedEvent, "docs" | "full">,
67
+ ): CommitCacheTags {
68
+ if (event.full) return { expire: [BACKMATTER_CACHE_TAG], refresh: [] };
69
+ const expire = new Set<string>();
70
+ const refresh = new Set<string>();
47
71
  for (const doc of event.docs ?? []) {
48
- for (const tag of contentCacheTags(doc.type, doc.slug)) {
49
- if (tag !== BACKMATTER_CACHE_TAG) tags.add(tag);
50
- }
72
+ expire.add(docCacheTag(doc.type, doc.slug));
73
+ refresh.add(typeCacheTag(doc.type));
51
74
  }
52
- return [...tags];
75
+ return { expire: [...expire], refresh: [...refresh] };
53
76
  }
54
77
 
55
78
  /** Site paths the host should `revalidatePath` for these docs. */
package/src/index.ts CHANGED
@@ -82,8 +82,16 @@ export type { DocFormat } from "./doc-format";
82
82
  export { modelFormat, parseDoc, serializeDoc } from "./doc-format";
83
83
  export { reserializeDoc } from "./reserialize";
84
84
 
85
- export type { BackmatterConfig, BackmatterDirs, DevBarHostConfig, ResolvedConfig } from "./config";
85
+ export type {
86
+ BackmatterConfig,
87
+ BackmatterDirs,
88
+ CacheLifeProfile,
89
+ DevBarHostConfig,
90
+ ResolvedConfig,
91
+ } from "./config";
86
92
  export {
93
+ cacheLifeProfile,
94
+ DEFAULT_CACHE_LIFE,
87
95
  defineConfig,
88
96
  resolveConfig,
89
97
  detectSiteUrl,
@@ -203,7 +211,7 @@ export type { MediaInfo, MediaInfoMap, MediaResolver } from "./media-info";
203
211
  export type { PublicDocsQuery } from "./api-loader";
204
212
  export { syncSchema, detectGitBranch } from "./sync-schema";
205
213
  export type { SyncSchemaOptions } from "./sync-schema";
206
- export type { ContentCommittedDoc, ContentCommittedEvent } from "./content-committed";
214
+ export type { CommitCacheTags, ContentCommittedDoc, ContentCommittedEvent } from "./content-committed";
207
215
  export {
208
216
  CONTENT_COMMITTED_EVENT,
209
217
  BACKMATTER_CACHE_TAG,
package/src/node/cli.ts CHANGED
@@ -277,6 +277,7 @@ export default defineConfig({
277
277
  function renderCatchAllPage(): string {
278
278
  return `// ${CATCH_ALL_DIR}/page.tsx — mounts every model at its route, including / via indexSlug.
279
279
  import { cacheLife } from "next/cache";
280
+ import { cacheLifeProfile } from "@backmatter/core";
280
281
  import { createCatchAll } from "@backmatter/next/routes";
281
282
  import { cms } from "${REGISTRY_SPECIFIER}";
282
283
  import { components as mdxComponents } from "${REGISTRY_SPECIFIER}/mdx";
@@ -295,7 +296,7 @@ async function cachedParams<T extends { backmatter?: string[] }>(
295
296
  params: Promise<T> | T,
296
297
  ): Promise<T> {
297
298
  "use cache";
298
- cacheLife("hours");
299
+ cacheLife(cacheLifeProfile(cms.config));
299
300
  return params;
300
301
  }
301
302
 
@@ -107,14 +107,14 @@ function renderCachedCmsClient(typeArgs: string): string[] {
107
107
  "",
108
108
  "async function cachedGetDoc(type: string, slug: string) {",
109
109
  ' "use cache";',
110
- ' cacheLife("hours");',
110
+ " cacheLife(cacheLifeProfile(config));",
111
111
  " cacheTag(...contentCacheTags(type, slug));",
112
112
  " return raw.getDoc(type, slug);",
113
113
  "}",
114
114
  "",
115
115
  "async function cachedListDocs(filter?: { type?: string }) {",
116
116
  ' "use cache";',
117
- ' cacheLife("hours");',
117
+ " cacheLife(cacheLifeProfile(config));",
118
118
  " cacheTag(...listCacheTags(filter?.type));",
119
119
  " return raw.listDocs(filter);",
120
120
  "}",
@@ -153,7 +153,7 @@ export function renderModelsBarrel(input: {
153
153
  const typeArgs = blocks.length > 0 ? "<typeof models, Blocks>" : "";
154
154
  const lines = [GENERATED_HEADER, `import { createBackmatter } from "${input.sdk}";`];
155
155
  if (nextHost) {
156
- lines.push('import { contentCacheTags, createQuery, listCacheTags } from "@backmatter/core";');
156
+ lines.push('import { cacheLifeProfile, contentCacheTags, createQuery, listCacheTags } from "@backmatter/core";');
157
157
  lines.push('import { currentPreviewContent } from "@backmatter/next/preview-content";');
158
158
  lines.push('import { cacheLife, cacheTag } from "next/cache";');
159
159
  }