@notion-headless-cms/core
v0.3.17
Published
Core CMS engine for notion-headless-cms — fetch, transform, cache with stale-while-revalidate strategy
Maintainers
Readme
@notion-headless-cms/core
CMS エンジン本体。createCMS を提供し、SWR / 更新検知 / フック /
リトライ / Web Standard Route Handler を内蔵する。外部ランタイム依存ゼロ。
インストール
pnpm add @notion-headless-cms/core @notion-headless-cms/notion-orm \
@notion-headless-cms/renderer @notionhq/client zod \
unified remark-parse remark-gfm remark-rehype rehype-stringifycore 自体は依存ゼロだが、実際に Notion を叩くには notion-orm、
HTML 化には renderer が必要。CLI 生成物 (nhc-schema.ts) が
notion-orm を自動参照する。
基本形
import { createCMS, nodePreset } from "@notion-headless-cms/core";
import { cmsDataSources } from "./generated/nhc-schema";
const cms = createCMS({
...nodePreset({ ttlMs: 5 * 60_000 }),
dataSources: cmsDataSources,
});
const posts = await cms.posts.getList();
const post = await cms.posts.getItem("my-post");
if (post) console.log(await post.content.html());公開 API
createCMS(opts)
dataSources: CLI が生成するcmsDataSources(必須)cache?:CacheConfig | "disabled"renderer?:RendererFn(未指定なら@notion-headless-cms/rendererを動的ロード)content?:imageProxyBase等waitUntil?: Cloudflare Workers のctx.waitUntilhooks?/plugins?/logger?/rateLimiter?
nodePreset(opts?)
Node.js 向けプリセット。{ cache: { document: memoryDocumentCache, image: memoryImageCache }, renderer } を返す。
cache?:CacheConfig | "disabled"— デフォルトは memory cachettlMs?: SWR TTL (ミリ秒)renderer?: カスタム renderer
BaseContentItem — 全コンテンツの基本型
CLI 生成の createCMS ラッパーで返される items は、スキーマで定義されたプロパティに加えて以下の自動フィールドを含む:
id: string— Notion ページ IDslug: string— コレクション設定のfields.slugで指定したプロパティから抽出title?: string | null— Notiontitle型プロパティ(自動検出)updatedAt: string— Notion ページの最終編集時刻(ISO-8601、キャッシュ更新判定に使用)lastEditedTime?: string— Notion のpage.last_edited_timeと同値。常にセットされるシステムフィールド(updatedAtと同じ値)status?: string | null—fields.statusで指定したプロパティpublishedAt?: string | null—fields.publishedAtで指定したプロパティ
cms.<collection> の主なメソッド
getItem(slug)— 本文込みで単件取得 (SWR)。返り値はT & { content: { blocks, html(), markdown() } }getList(opts?)— 公開済み一覧 (本文なし)getStaticParams()/getStaticPaths()— SSG 用adjacent(slug, opts?)— 前後記事ナビゲーションrevalidate(scope?)/prefetch(opts?)— コレクション別キャッシュ操作
グローバル操作
cms.$collections— コレクション名一覧cms.$revalidate(scope?)—"all" | { collection } | { collection, slug }cms.$handler(opts?)— Web Standard(req) => Responseを返す (画像プロキシ + webhook)cms.$getCachedImage(hash)— 画像キャッシュ直アクセスcms.cacheImage(url)— Notion 画像 URL を{imageProxyBase}/{sha256}形式に変換しキャッシュへ書き込む。画像キャッシュ未設定ならundefined。@notion-headless-cms/react-renderer/serverのresolveBlockImageUrlsなどサーバー側で URL 書き換えに使うcms.imageProxyBase— 画像プロキシのベース URL (createCMS({ imageProxyBase })、デフォルト/api/images)
cache アダプタ
memoryDocumentCache({ maxItems? })— LRU 対応インメモリキャッシュmemoryImageCache({ maxItems?, maxSizeBytes? })noopDocumentCache()/noopImageCache()
エラー
CMSError/isCMSError/isCMSErrorInNamespace(err, "core/")- 名前空間:
core/*/source/*/cache/*/renderer/*/cli/*
サブパスエクスポート
import { CMSError } from "@notion-headless-cms/core/errors";
import { memoryDocumentCache } from "@notion-headless-cms/core/cache/memory";| サブパス | 内容 |
|---|---|
| @notion-headless-cms/core | 全エクスポート |
| @notion-headless-cms/core/errors | CMSError / isCMSError / isCMSErrorInNamespace |
| @notion-headless-cms/core/hooks | mergeHooks / mergeLoggers |
| @notion-headless-cms/core/cache/memory | memoryDocumentCache / memoryImageCache |
| @notion-headless-cms/core/cache/noop | noopDocumentCache / noopImageCache |
ランタイム別レシピ
- Node.js スクリプト
- Cloudflare Workers + R2 + KV (
cloudflarePresetはcache-r2パッケージ) - Next.js App Router
