@backmatter/core 0.0.42 → 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 +1 -1
- package/src/block.ts +5 -0
- package/src/config.ts +20 -0
- package/src/content-committed.ts +34 -11
- package/src/index.ts +10 -2
- package/src/node/cli.ts +2 -1
- package/src/node/eval-block.ts +13 -2
- package/src/node/generate.ts +3 -3
- package/src/node/local-modules.ts +23 -1
package/package.json
CHANGED
package/src/block.ts
CHANGED
|
@@ -106,6 +106,11 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
|
106
106
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/** The examples in `value` that have a name and an object of props; the rest are dropped. */
|
|
110
|
+
export function validBlockExamples(value: unknown): BlockExample[] {
|
|
111
|
+
return readBlockExamples(value, false) ?? [];
|
|
112
|
+
}
|
|
113
|
+
|
|
109
114
|
/** Valid examples; null when `strict` and any entry is bad (otherwise bad ones are dropped). */
|
|
110
115
|
function readBlockExamples(value: unknown, strict: boolean): BlockExample[] | null {
|
|
111
116
|
if (!Array.isArray(value)) return strict ? null : [];
|
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
|
+
}
|
package/src/content-committed.ts
CHANGED
|
@@ -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,
|
|
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,
|
|
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
|
|
41
|
-
*
|
|
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(
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
72
|
+
expire.add(docCacheTag(doc.type, doc.slug));
|
|
73
|
+
refresh.add(typeCacheTag(doc.type));
|
|
51
74
|
}
|
|
52
|
-
return [...
|
|
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 {
|
|
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(
|
|
299
|
+
cacheLife(cacheLifeProfile(cms.config));
|
|
299
300
|
return params;
|
|
300
301
|
}
|
|
301
302
|
|
package/src/node/eval-block.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// SaaS never loads customer React, Next, or the host checkout.
|
|
5
5
|
import { runInNewContext } from "node:vm";
|
|
6
6
|
import { field } from "../fields";
|
|
7
|
-
import { defineBlock, type BlockDef } from "../block";
|
|
7
|
+
import { defineBlock, validBlockExamples, type BlockDef } from "../block";
|
|
8
8
|
import { compileLocalModules, pickBindings, withLocalModules, type LocalModules } from "./local-modules";
|
|
9
9
|
|
|
10
10
|
const SOURCE_EXT = /\.(ts|tsx|js|jsx|mjs)$/;
|
|
@@ -143,6 +143,17 @@ export type BlockSourceOptions = {
|
|
|
143
143
|
files?: ReadonlyMap<string, string>;
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Examples only dress the block library. When a site helper that builds them
|
|
148
|
+
* comes back stubbed or wrong in the sandbox, drop the bad ones, not the block.
|
|
149
|
+
*/
|
|
150
|
+
function defineBlockKeepingValidExamples(def: Record<string, unknown>): BlockDef {
|
|
151
|
+
if (def.examples === undefined) return defineBlock(def as Parameters<typeof defineBlock>[0]);
|
|
152
|
+
const { examples, ...rest } = def;
|
|
153
|
+
const valid = validBlockExamples(examples);
|
|
154
|
+
return defineBlock((valid.length > 0 ? { ...rest, examples: valid } : rest) as Parameters<typeof defineBlock>[0]);
|
|
155
|
+
}
|
|
156
|
+
|
|
146
157
|
function transpile(source: string): string {
|
|
147
158
|
const transpiler = new Bun.Transpiler({ loader: "tsx", target: "node" });
|
|
148
159
|
return transpiler.transformSync(source);
|
|
@@ -171,7 +182,7 @@ export function evalBlockSource(
|
|
|
171
182
|
jsxDEV: (...args: unknown[]) => null;
|
|
172
183
|
};
|
|
173
184
|
const el = () => null;
|
|
174
|
-
sandbox.defineBlock = defineBlock;
|
|
185
|
+
sandbox.defineBlock = defineBlockKeepingValidExamples as typeof defineBlock;
|
|
175
186
|
sandbox.field = field;
|
|
176
187
|
sandbox.React = { createElement: el, Fragment: "Fragment" };
|
|
177
188
|
sandbox.jsx = el;
|
package/src/node/generate.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// files (`../lib/block-fields`). The block sandbox runs those files too, under
|
|
4
4
|
// the same rules, so their exports carry real values instead of stubs. A file
|
|
5
5
|
// that is missing, breaks a rule, or throws falls back to stubs for its names.
|
|
6
|
+
// A `.json` import (content a site reuses for block examples) is plain data.
|
|
6
7
|
|
|
7
8
|
const SOURCE_EXTS = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
|
|
8
9
|
const INDEX_FILES = ["/index.ts", "/index.tsx", "/index.js"];
|
|
@@ -115,7 +116,9 @@ function compileModule(repoPath: string, ctx: CompileContext): number | null {
|
|
|
115
116
|
// A cycle, or too many files: the importer gets stubs for this one.
|
|
116
117
|
if (ctx.inProgress.has(repoPath) || ctx.indexes.size >= MAX_MODULES) return null;
|
|
117
118
|
const source = ctx.files.get(repoPath);
|
|
118
|
-
if (source === undefined
|
|
119
|
+
if (source === undefined) return null;
|
|
120
|
+
if (repoPath.endsWith(".json")) return compileJson(repoPath, source, ctx);
|
|
121
|
+
if (ctx.isForbidden(source)) return null;
|
|
119
122
|
|
|
120
123
|
let js: string;
|
|
121
124
|
try {
|
|
@@ -151,6 +154,25 @@ function compileModule(repoPath: string, ctx: CompileContext): number | null {
|
|
|
151
154
|
return index;
|
|
152
155
|
}
|
|
153
156
|
|
|
157
|
+
/**
|
|
158
|
+
* A JSON file is data: its value is the default export, and an object's
|
|
159
|
+
* top-level keys are named exports, as bundlers do. Invalid JSON gets stubs.
|
|
160
|
+
*/
|
|
161
|
+
function compileJson(repoPath: string, source: string, ctx: CompileContext): number | null {
|
|
162
|
+
let value: unknown;
|
|
163
|
+
try {
|
|
164
|
+
value = JSON.parse(source);
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const index = ctx.indexes.size;
|
|
169
|
+
ctx.indexes.set(repoPath, index);
|
|
170
|
+
ctx.chunks.push(
|
|
171
|
+
`__mods[${index}] = (function (d) { var m = d && typeof d === "object" && !Array.isArray(d) ? Object.assign({}, d) : {}; m["default"] = d; return m; })(${JSON.stringify(value)});`,
|
|
172
|
+
);
|
|
173
|
+
return index;
|
|
174
|
+
}
|
|
175
|
+
|
|
154
176
|
/** Strip `export` keywords and list [exported name, local expression] pairs. */
|
|
155
177
|
function collectExports(js: string): { body: string; names: Array<[string, string]> } {
|
|
156
178
|
const names: Array<[string, string]> = [];
|