Prepr CMS & Astro
本頁內容尚未翻譯。
Prepr CMS is a headless CMS with built-in personalization.
Integrating with Astro
Section titled Integrating with AstroPrepr CMS provides a GraphQL API to connect your data to Astro.
Prerequisites
Section titled PrerequisitesTo get started, you will need the following:
- A new or existing Astro project configured for on-demand rendering.
- A free Prepr account
- A Prepr environment with existing blog posts. These posts must include a
title
andcontent
. Other fields are optional.
Setting up credentials
Section titled Setting up credentialsTo add the Prepr endpoint to your Astro project, create a .env file
in the root of your project if one does not already exist and add the following variable:
You will find your YOUR_PREPR_API_URL
value from your Prepr account settings:
-
Go to Settings > Access tokens to view both your Preview and Production access tokens.
-
Use the API URL value from the GraphQL Production access token to only retrieve published content items for your Astro site.
Configuring the Prepr endpoint
Section titled Configuring the Prepr endpointCreate a new folder src/lib/
and add a new file called prepr.js
. This is where you will configure the Prepr endpoint. Add the following code to fetch your data from Prepr CMS:
Your root directory should now include these files:
目錄lib/
- prepr.js
目錄src/
- …
- .env
- astro.config.mjs
- package.json
Fetching data
Section titled Fetching dataYou will fetch your data from Prepr by writing queries to interact with its GraphQL API.
Create a GraphQL query to retrieve your blog articles:
Section titled Create a GraphQL query to retrieve your blog articles:-
Create a new folder
src/queries/
and add a file namedget-articles.js
. -
Add the following query to this file to retrieve all articles:
-
To display a linked list of your blog posts on a page, import and execute your query, including the necessary Prepr endpoint. You will then have access to all your posts titles and their slugs to render to the page. (In the next step, you will create individual pages for your blog posts.)
Your root directory should include these new files:
目錄lib/
- prepr.js
目錄queries /
- get-articles.js
目錄src/
- …
- .env
- astro.config.mjs
- package.json
Creating individual blog post pages
Section titled Creating individual blog post pagesTo create a page for each blog post, you will execute a new GraphQL query on a dynamic routing .astro
page. This query will fetch a specific article by its slug and a new page will be created for each individual blog post.
-
Create a file called
get-article-by-slug.js
in thequeries
folder and add the following to query a specific article by its slug and return data such as the articletitle
andcontent
:You can create and test GraphQL queries using the Apollo explorer in Prepr. Open the API Explorer from the Article content item page in Prepr. The Article content is stored in a Dynamic content field. Check out the GraphQL docs for more details on how to fetch the data within this field.
-
Inside the
src/pages
folder, create a file called[…slug].astro
. Add the following code to import and execute the query from the previous step and display the retrieved article:
Your root directory should now include these new files:
目錄lib/
- prepr.js
目錄queries/
- get-articles.js
- get-article-by-slug.js
目錄src/
目錄pages/
- index.astro
- […slug].astro
- .env
- astro.config.mjs
- package.json
Now, when you click an article link from the main list of blog posts, you will be taken to a page with its individual content.
Publishing your site
Section titled Publishing your siteTo deploy your Prepr blog, visit our deployment guides and follow the instructions for your preferred hosting provider.
Official Resources
Section titled Official Resources- Follow the Prepr CMS Astro quickstart guide to make a simple blog with Astro and Prepr CMS.
- Check out the Connecting Prepr CMS to Astro for an overview of Astro and Prepr CMS resources.