@backmatter/core 0.0.9 → 0.0.11
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/doc.ts +2 -0
- package/src/node/cli.ts +25 -2
- package/src/node/fs-loader.ts +10 -0
- package/src/node/generate.ts +39 -2
- package/src/query.ts +15 -2
package/package.json
CHANGED
package/src/doc.ts
CHANGED
package/src/node/cli.ts
CHANGED
|
@@ -260,6 +260,7 @@ export default defineConfig({
|
|
|
260
260
|
|
|
261
261
|
function renderCatchAllPage(registryImport: string): string {
|
|
262
262
|
return `// ${CATCH_ALL_DIR}/page.tsx — mounts every model at its route, including / via indexSlug.
|
|
263
|
+
import { cacheLife } from "next/cache";
|
|
263
264
|
import { createCatchAll } from "@backmatter/next/routes";
|
|
264
265
|
import { cms } from "${registryImport}";
|
|
265
266
|
import { components } from "${registryImport}/mdx";
|
|
@@ -270,8 +271,30 @@ import { layouts } from "${registryImport}/layouts";
|
|
|
270
271
|
const route = createCatchAll(cms, { components, blocks: blockLoaders, fallbacks: blockFallbacks, layouts });
|
|
271
272
|
|
|
272
273
|
export const generateStaticParams = route.generateStaticParams;
|
|
273
|
-
|
|
274
|
-
|
|
274
|
+
|
|
275
|
+
async function cachedParams<T extends { backmatter?: string[] }>(
|
|
276
|
+
params: Promise<T> | T,
|
|
277
|
+
): Promise<T> {
|
|
278
|
+
"use cache";
|
|
279
|
+
cacheLife("hours");
|
|
280
|
+
return params;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export async function generateMetadata(props: {
|
|
284
|
+
params: Promise<{ backmatter?: string[] }>;
|
|
285
|
+
}) {
|
|
286
|
+
return route.generateMetadata({ params: cachedParams(props.params) });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export default async function Page(props: {
|
|
290
|
+
params: Promise<{ backmatter?: string[] }>;
|
|
291
|
+
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
|
292
|
+
}) {
|
|
293
|
+
return route.Page({
|
|
294
|
+
params: cachedParams(props.params),
|
|
295
|
+
searchParams: props.searchParams,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
275
298
|
`;
|
|
276
299
|
}
|
|
277
300
|
|
package/src/node/fs-loader.ts
CHANGED
|
@@ -53,10 +53,20 @@ export async function listDocsFromFs(
|
|
|
53
53
|
const rel = path.relative(siteRoot, full).split(path.sep).join("/");
|
|
54
54
|
const hit = matchAnyModelPath(models, rel);
|
|
55
55
|
if (!hit) continue;
|
|
56
|
+
const model = models.find((m) => m.type === hit.type);
|
|
57
|
+
let frontmatter: Record<string, unknown> | undefined;
|
|
58
|
+
if (model) {
|
|
59
|
+
try {
|
|
60
|
+
frontmatter = parseDoc(await readFile(full, "utf-8"), modelFormat(model)).frontmatter;
|
|
61
|
+
} catch {
|
|
62
|
+
frontmatter = undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
56
65
|
docs.push({
|
|
57
66
|
locale: hit.locale,
|
|
58
67
|
type: hit.type,
|
|
59
68
|
slug: hit.slug,
|
|
69
|
+
frontmatter,
|
|
60
70
|
});
|
|
61
71
|
}
|
|
62
72
|
return docs.sort((a, b) => {
|
package/src/node/generate.ts
CHANGED
|
@@ -93,13 +93,46 @@ export function detectSdk(root: string): string {
|
|
|
93
93
|
return "@backmatter/core/node";
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
function renderCachedCmsClient(): string[] {
|
|
97
|
+
return [
|
|
98
|
+
"const raw = createBackmatter({ ...config, models });",
|
|
99
|
+
"",
|
|
100
|
+
"async function getDoc(locale: string, type: string, slug: string) {",
|
|
101
|
+
' "use cache";',
|
|
102
|
+
' cacheLife("hours");',
|
|
103
|
+
" cacheTag(...contentCacheTags(locale, type, slug));",
|
|
104
|
+
" return raw.getDoc(locale, type, slug);",
|
|
105
|
+
"}",
|
|
106
|
+
"",
|
|
107
|
+
"async function listDocs(filter?: { type?: string; locale?: string }) {",
|
|
108
|
+
' "use cache";',
|
|
109
|
+
' cacheLife("hours");',
|
|
110
|
+
" cacheTag(...listCacheTags(filter?.type));",
|
|
111
|
+
" return raw.listDocs(filter);",
|
|
112
|
+
"}",
|
|
113
|
+
"",
|
|
114
|
+
"/** Typed content client: cms.query.{type}, cms.getDoc, cms.listDocs. */",
|
|
115
|
+
"export const cms = {",
|
|
116
|
+
" ...raw,",
|
|
117
|
+
" getDoc,",
|
|
118
|
+
" listDocs,",
|
|
119
|
+
` query: createQuery(models, { getDoc, listDocs }),`,
|
|
120
|
+
"};",
|
|
121
|
+
];
|
|
122
|
+
}
|
|
123
|
+
|
|
96
124
|
export function renderModelsBarrel(input: {
|
|
97
125
|
outDir: string;
|
|
98
126
|
models: ModelFile[];
|
|
99
127
|
configFile: string | null;
|
|
100
128
|
sdk: string;
|
|
101
129
|
}): string {
|
|
130
|
+
const nextHost = input.sdk === "@backmatter/next";
|
|
102
131
|
const lines = [GENERATED_HEADER, `import { createBackmatter } from "${input.sdk}";`];
|
|
132
|
+
if (nextHost) {
|
|
133
|
+
lines.push('import { contentCacheTags, createQuery, listCacheTags } from "@backmatter/core";');
|
|
134
|
+
lines.push('import { cacheLife, cacheTag } from "next/cache";');
|
|
135
|
+
}
|
|
103
136
|
if (input.configFile) {
|
|
104
137
|
lines.push(`import config from "${importSpecifier(input.outDir, input.configFile)}";`);
|
|
105
138
|
} else {
|
|
@@ -112,8 +145,12 @@ export function renderModelsBarrel(input: {
|
|
|
112
145
|
lines.push(`export const models = [${input.models.map((m) => m.importName).join(", ")}] as const;`);
|
|
113
146
|
lines.push("export type Models = typeof models;");
|
|
114
147
|
lines.push("");
|
|
115
|
-
|
|
116
|
-
|
|
148
|
+
if (nextHost) {
|
|
149
|
+
lines.push(...renderCachedCmsClient());
|
|
150
|
+
} else {
|
|
151
|
+
lines.push("/** Typed content client: cms.query.{type}, cms.getDoc, cms.listDocs. */");
|
|
152
|
+
lines.push("export const cms = createBackmatter({ ...config, models });");
|
|
153
|
+
}
|
|
117
154
|
return `${lines.join("\n")}\n`;
|
|
118
155
|
}
|
|
119
156
|
|
package/src/query.ts
CHANGED
|
@@ -103,8 +103,21 @@ export function createQuery<const Models extends readonly ModelDef[]>(
|
|
|
103
103
|
docs = doc ? [doc] : [];
|
|
104
104
|
} else {
|
|
105
105
|
const summaries = await loader.listDocs({ type, locale: opts?.locale });
|
|
106
|
-
const
|
|
107
|
-
|
|
106
|
+
const fromList = summaries.filter((s) => s.frontmatter != null);
|
|
107
|
+
const missing = summaries.filter((s) => s.frontmatter == null);
|
|
108
|
+
const loaded = await Promise.all(missing.map((s) => get(type, s.locale, s.slug)));
|
|
109
|
+
docs = [
|
|
110
|
+
...fromList.map((s) => ({
|
|
111
|
+
locale: s.locale,
|
|
112
|
+
type: s.type,
|
|
113
|
+
slug: s.slug,
|
|
114
|
+
source: "head" as const,
|
|
115
|
+
frontmatter: normalizeFrontmatter(s.frontmatter ?? {}),
|
|
116
|
+
body: "",
|
|
117
|
+
raw: "",
|
|
118
|
+
})),
|
|
119
|
+
...loaded.filter((d): d is BackmatterDoc => d !== null),
|
|
120
|
+
];
|
|
108
121
|
}
|
|
109
122
|
|
|
110
123
|
const filtered = opts?.where ? docs.filter((d) => matchesWhere(d, opts.where!)) : docs;
|