Integrate MarbleCMS in your Astro application
Set up environment variables
.env
file in the root of your Astro project and add the following:MARBLE_WORKSPACE_KEY
in client-side code.
Your environment variables are loaded into the server context during the build
process and should not be shared with the client. For client-side updates, use
webhooks to trigger rebuilds.Configure your content collection
src/content/config.ts
and add the following code.This configuration defines a posts
collection, specifies a schema for your post data using Zod for validation, and includes a loader function that fetches all your posts from the MarbleCMS API.Display a list of posts
getCollection()
to fetch all your posts and display them on a page. Create a file at src/pages/blog/index.astro
to list all your posts.Display a single post
src/pages/blog/[...slug].astro
.This page uses getStaticPaths()
to generate a static page for every post at build time.set:html
directive to render the post content. MarbleCMS sanitizes all HTML content on the server, ensuring it’s safe to render directly in your application.