@backmatter/core 0.0.10 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/core",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
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
- export const generateMetadata = route.generateMetadata;
274
- export default route.Page;
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
 
@@ -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
- lines.push("/** Typed content client: cms.query.{type}, cms.getDoc, cms.listDocs. */");
116
- lines.push("export const cms = createBackmatter({ ...config, models });");
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