Keystatic & Astro
This content is not available in your language yet.
Keystatic is an open source, headless content-management system that allows you to structure your content and sync it with GitHub.
If you’re starting a new Astro + Keystatic project from scratch, you can use the Keystatic CLI to generate a new project in seconds:
Select the Astro template, and you’ll be ready to deploy!
Prerequisites
Section titled Prerequisites- An existing Astro project with server-side rendering (SSR) and
output: 'hybrid'
oroutput: 'server'
configured.
If you intend to sync Keystatic’s data with GitHub, you will also need a GitHub account with write
permissions on the repository for this project.
Installing dependencies
Section titled Installing dependenciesAdd both the Markdoc (for content entries) and the React (for the Keystatic Admin UI Dashboard) integrations to your Astro project, using the astro add
command for your package manager.
You will also need two Keystatic packages:
Adding the Astro integration
Section titled Adding the Astro integrationAdd the Astro integration from @keystatic/astro
in your Astro config file:
Creating a Keystatic config file
Section titled Creating a Keystatic config fileA Keystatic config file is required to define your content schema. This file will also allow you to connect a project to a specific GitHub repository (if you decide to do so).
Create a file called keystatic.config.ts
in the root of the project and add the following code to define both your storage type (local
) and a single content collection (posts
):
If you are already using content collections in your Astro project, then update the schema above to exactly match the collection(s) defined in your existing schema.
Keystatic is now configured to manage your content based on your schema.
Running Keystatic locally
Section titled Running Keystatic locallyTo launch your Keystatic Admin UI dashboard, start Astro’s dev server:
Visit http://127.0.0.1:4321/keystatic
in the browser to see the Keystatic Admin UI running.
Creating a new post
Section titled Creating a new post-
In the Keystatic Admin UI dashboard, click on the “Posts” collection.
-
Use the button to create a new post. Add the title “My First Post” and some content, then save the post.
-
This post should now be visible from your “Posts” collection. You can view and edit your individual posts from this dashboard page.
-
Return to view your Astro project files. You will now find a new
.mdoc
file inside thesrc/content/posts
directory for this new post:Directorysrc/
Directorycontent/
Directoryposts/
- my-first-post.mdoc
-
Navigate to that file in your code editor and verify that you can see the Markdown content you entered. For example:
Rendering Keystatic content
Section titled Rendering Keystatic contentUse Astro’s Content Collections API to query and display your posts and collections, just as you would in any Astro project.
Displaying a collection list
Section titled Displaying a collection listThe following example displays a list of each post title, with a link to an individual post page.
Displaying a single entry
Section titled Displaying a single entryTo display content from an individual post, you can import and use the <Content />
component to render your content to HTML:
For more information on querying, filtering, displaying your collections content and more, see the full content collections documentation.
Deploying Keystatic + Astro
Section titled Deploying Keystatic + AstroTo deploy your website, visit our deployment guides and follow the instructions for your preferred hosting provider.
You’ll also probably want to connect Keystatic to GitHub so you can manage content on the deployed instance of the project.