Prepr CMS 与 Astro
Prepr CMS 是一个内置个性化功能的无头 CMS。
与 Astro 集成
标题为“与 Astro 集成”的部分Prepr CMS 提供了一个 GraphQL API 来将您的数据连接到 Astro。
先决条件
标题为“先决条件”的部分要开始使用,您需要以下内容
- 一个配置为按需渲染的新的或现有的 Astro 项目。
- 一个免费的 Prepr 帐户
- 一个已有博客文章的 Prepr 环境。这些文章必须包含
title
和content
。其他字段是可选的。
设置凭据
标题为“设置凭据”的部分要将 Prepr 端点添加到您的 Astro 项目中,请在项目根目录中创建一个 .env
文件(如果尚不存在),并添加以下变量
PREPR_ENDPOINT=YOUR_PREPR_API_URL
您可以在 Prepr 帐户设置中找到您的 YOUR_PREPR_API_URL
值
-
转到 设置 > 访问令牌 以查看您的预览和生产访问令牌。
-
使用 GraphQL Production 访问令牌中的 API URL 值,以便仅为您的 Astro 站点检索已发布的内容项。
配置 Prepr 端点
标题为“配置 Prepr 端点”的部分创建一个新文件夹 src/lib/
并添加一个名为 prepr.js
的新文件。您将在这里配置 Prepr 端点。添加以下代码以从 Prepr CMS 获取您的数据
export async function Prepr(query, variables) { const response = await fetch(import.meta.env.PREPR_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query, variables }), }) return response}
您的根目录现在应包含以下这些文件
目录src/
目录lib/
- prepr.js
- .env
- astro.config.mjs
- package.json
获取数据
标题为“获取数据”的部分您将通过编写查询与 Prepr 的 GraphQL API 交互来获取数据。
创建 GraphQL 查询以检索您的博客文章
标题为“创建 GraphQL 查询以检索您的博客文章:”的部分-
创建一个新文件夹
src/queries/
并添加一个名为get-articles.js
的文件。 -
将以下查询添加到此文件中以检索所有文章
src/queries/get-articles.js const GetArticles = `query {Articles {items {_id_slugtitle}}}`export default GetArticles -
要在页面上显示博客文章的链接列表,请导入并执行您的查询,包括必要的 Prepr 端点。然后,您将可以访问所有文章的标题及其 slug 以在页面上渲染。(在下一步中,您将为您的博客文章创建单独的页面。)
src/pages/index.astro ---import Layout from '../layouts/Layout.astro';import { Prepr } from '../lib/prepr.js';import GetArticles from '../queries/get-articles.js';const response = await Prepr(GetArticles)const { data } = await response.json()const articles = data.Articles---<Layout title="My blog site"><h1>My blog site</h1><ul>{articles.items.map((post) => (<li><a href={post._slug}>{post.title}</a></li>))}</ul></Layout>
您的根目录现在应包含以下这些新文件
目录src/
目录lib/
- prepr.js
目录pages/
- index.astro
目录queries /
- get-articles.js
- .env
- astro.config.mjs
- package.json
创建独立的博客文章页面
标题为“创建独立的博客文章页面”的部分要为每篇博客文章创建一个页面,您将在一个动态路由 .astro
页面上执行一个新的 GraphQL 查询。此查询将通过其 slug 获取特定文章,并为每篇独立的博客文章创建一个新页面。
-
在
queries
文件夹中创建一个名为get-article-by-slug.js
的文件,并添加以下内容以按其 slug 查询特定文章并返回文章的title
和content
等数据src/lib/queries/get-article-by-slug.js const GetArticleBySlug = `query ($slug: String) {Article (slug: $slug) {_idtitlecontent {__typename... on Text {bodytext}... on Assets {items {url}}}}}`export default GetArticleBySlug您可以在 Prepr 中使用 Apollo explorer 创建和测试 GraphQL 查询。从 Prepr 中的 Article 内容项页面打开 API Explorer。文章内容存储在动态内容字段中。有关如何获取此字段内数据的更多详细信息,请查看 GraphQL 文档。
-
在
src/pages
文件夹中,创建一个名为[…slug].astro
的文件。添加以下代码以导入并执行上一步中的查询,并显示检索到的文章src/pages/[...slug].astro ---import Layout from '../layouts/Layout.astro';import {Prepr} from '../lib/prepr.js';import GetArticleBySlug from '../queries/get-article-by-slug.js';const { slug } = Astro.params;const response = await Prepr(GetArticleBySlug, {slug})const { data } = await response.json()const article = data.Article---<Layout title={article.title}><main><h1>{article.title}</h1>{article.content.map((content) => (<div>{content.__typename === "Assets" &&<img src={content.items[0].url} width="300" height="250"/>}{content.__typename === 'Text' &&<div set:html={content.body}></div>}</div>))}</main></Layout>
你的根目录现在应包含这些新文件
目录src/
目录lib/
- prepr.js
目录pages/
- index.astro
- […slug].astro
目录queries/
- get-articles.js
- get-article-by-slug.js
- .env
- astro.config.mjs
- package.json
现在,当您从博客文章主列表中单击文章链接时,您将被带到一个包含其独立内容的页面。
发布你的站点
标题为“发布您的站点”的部分要部署您的 Prepr 博客,请访问我们的部署指南,并按照您首选的托管提供商的说明进行操作。
官方资源
标题为“官方资源”的部分- 遵循 Prepr CMS Astro 快速入门指南,使用 Astro 和 Prepr CMS 制作一个简单的博客。
- 查看将 Prepr CMS 连接到 Astro,了解 Astro 和 Prepr CMS 资源的概览。