
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
dynamic-cms-sdk
Advanced tools
Dynamic CMS SDK for Next.js and React applications - 让前端接入 CMS 变得简单!
npm install dynamic-cms-sdk
# 或
yarn add dynamic-cms-sdk
// lib/cms.ts
import { initCMS } from '@dynamic-cms/sdk';
export const cms = initCMS({
baseURL: 'https://your-cms-api.com/functions/v1',
apiKey: 'your-api-key',
websiteId: 'your-website-id',
webhookSecret: 'your-webhook-secret', // 可选
});
import { useArticles, useArticle } from '@dynamic-cms/sdk';
// 文章列表
function BlogList() {
const { articles, loading, error } = useArticles({
status: 'published',
limit: 10,
});
if (loading) return <div>加载中...</div>;
if (error) return <div>错误: {error.message}</div>;
return (
<div>
{articles.map(article => (
<div key={article.id}>
<h2>{article.content.title}</h2>
<p>{article.content.excerpt}</p>
</div>
))}
</div>
);
}
// 单篇文章
function ArticlePage({ slug }: { slug: string }) {
const { article, loading, error } = useArticle(slug);
if (loading) return <div>加载中...</div>;
if (error) return <div>错误: {error.message}</div>;
if (!article) return <div>文章不存在</div>;
return (
<article>
<h1>{article.content.title}</h1>
<div dangerouslySetInnerHTML={{ __html: article.content.content }} />
</article>
);
}
import { ArticleList, SearchBox } from '@dynamic-cms/sdk';
function BlogPage() {
const { articles, loading, error } = useArticles();
return (
<div>
<SearchBox onSearch={(query) => console.log(query)} />
<ArticleList
articles={articles}
loading={loading}
error={error}
cardProps={{
showExcerpt: true,
showImage: true,
onTitleClick: (article) => {
window.location.href = `/blog/${article.slug}`;
}
}}
/>
</div>
);
}
// pages/blog/index.tsx
import { createGetStaticProps } from '@dynamic-cms/sdk/nextjs';
import { cms } from '@/lib/cms';
export const getStaticProps = createGetStaticProps(cms, {
status: 'published',
limit: 20,
});
// pages/blog/[slug].tsx
import { createGetStaticProps, createGetStaticPaths } from '@dynamic-cms/sdk/nextjs';
export const getStaticProps = createGetStaticPropsForArticle(cms);
export const getStaticPaths = createGetStaticPaths(cms);
// pages/api/webhook/cms.ts
import { createWebhookHandler } from '@dynamic-cms/sdk/nextjs';
import { cms } from '@/lib/cms';
export default createWebhookHandler(cms, process.env.CMS_WEBHOOK_SECRET!, {
onArticlePublished: async (article) => {
console.log('文章发布:', article.title);
},
onArticleUpdated: async (article) => {
console.log('文章更新:', article.title);
},
onArticleDeleted: async (article) => {
console.log('文章删除:', article.title);
},
});
SDK 提供了基础的 CSS 类名,你可以自定义样式:
/* 文章卡片 */
.cms-article-card {
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 1rem;
margin-bottom: 1rem;
}
.cms-article-title {
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
.cms-article-excerpt {
color: #6b7280;
margin-bottom: 1rem;
}
/* 分页 */
.cms-pagination {
display: flex;
gap: 0.5rem;
justify-content: center;
margin-top: 2rem;
}
.cms-pagination button {
padding: 0.5rem 1rem;
border: 1px solid #d1d5db;
background: white;
cursor: pointer;
}
.cms-pagination button.active {
background: #3b82f6;
color: white;
}
useArticles(params) - 获取文章列表useArticle(slug) - 获取单篇文章useSearchArticles(query, params) - 搜索文章useWebsite() - 获取网站配置useRelatedArticles(articleId, limit) - 获取相关文章usePagination(total, pageSize) - 分页逻辑<ArticleCard /> - 文章卡片<ArticleList /> - 文章列表<Pagination /> - 分页组件<SearchBox /> - 搜索框generateExcerpt() - 生成摘要calculateReadingTime() - 计算阅读时间formatDate() - 格式化日期generateSlug() - 生成 URL 别名verifyWebhookSignature() - 验证 Webhook 签名SDK 完全使用 TypeScript 编写,提供完整的类型定义:
import { Article, CMSConfig, ArticleListParams } from '@dynamic-cms/sdk';
const config: CMSConfig = {
baseURL: 'https://api.example.com',
apiKey: 'your-key',
websiteId: 'your-id',
};
const params: ArticleListParams = {
status: 'published',
limit: 10,
category: 'tech',
};
MIT License
欢迎提交 Issue 和 Pull Request!
FAQs
Dynamic CMS SDK for Next.js and React applications
We found that dynamic-cms-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.