Contentful & Astro
Este conteúdo não está disponível em sua língua ainda.
Contentful is a headless CMS that allows you to manage content, integrate with other services, and publish to multiple platforms.
Integrating with Astro
Section titled Integrating with AstroIn this section, we’ll use the Contentful SDK to connect your Contentful space to Astro with zero client-side JavaScript.
Prerequisites
Section titled PrerequisitesTo get started, you will need to have the following:
-
An Astro project - If you don’t have an Astro project yet, our Installation guide will get you up and running in no time.
-
A Contentful account and a Contentful space. If you don’t have an account, you can sign up for a free account and create a new Contentful space. You can also use an existing space if you have one.
-
Contentful credentials - You can find the following credentials in your Contentful dashboard Settings > API keys. If you don’t have any API keys, create one by selecting Add API key.
- Contentful space ID - The ID of your Contentful space.
- Contentful delivery access token - The access token to consume published content from your Contentful space.
- Contentful preview access token - The access token to consume unpublished content from your Contentful space.
Setting up credentials
Section titled Setting up credentialsTo add your Contentful space’s credentials to Astro, create an .env
file in the root of your project with the following variables:
CONTENTFUL_SPACE_ID=YOUR_SPACE_IDCONTENTFUL_DELIVERY_TOKEN=YOUR_DELIVERY_TOKENCONTENTFUL_PREVIEW_TOKEN=YOUR_PREVIEW_TOKEN
Now, you can use these environment variables in your project.
If you would like to have IntelliSense for your Contentful environment variables, you can create a env.d.ts
file in the src/
directory and configure ImportMetaEnv
like this:
interface ImportMetaEnv { readonly CONTENTFUL_SPACE_ID: string; readonly CONTENTFUL_DELIVERY_TOKEN: string; readonly CONTENTFUL_PREVIEW_TOKEN: string;}
Read more about using environment variables and .env
files in Astro.
Your root directory should now include these new files:
Directorysrc/
- env.d.ts
- .env
- astro.config.mjs
- package.json
Installing dependencies
Section titled Installing dependenciesTo connect with your Contentful space, install both of the following using the single command below for your preferred package manager:
contentful.js
, the official Contentful SDK for JavaScriptrich-text-html-renderer
, a package to render Contentful’s rich text fields to HTML.
npm install contentful @contentful/rich-text-html-renderer
pnpm add contentful @contentful/rich-text-html-renderer
yarn add contentful @contentful/rich-text-html-renderer
Next, create a new file called contentful.ts
in the src/lib/
directory of your project.
import * as contentful from "contentful";
export const contentfulClient = contentful.createClient({ space: import.meta.env.CONTENTFUL_SPACE_ID, accessToken: import.meta.env.DEV ? import.meta.env.CONTENTFUL_PREVIEW_TOKEN : import.meta.env.CONTENTFUL_DELIVERY_TOKEN, host: import.meta.env.DEV ? "preview.contentful.com" : "cdn.contentful.com",});
The above code snippet creates a new Contentful client, passing in credentials from the .env
file.
While in development mode, your content will be fetched from the Contentful preview API. This means that you will be able to see unpublished content from the Contentful web app.
At build time, your content will be fetched from the Contentful delivery API. This means that only published content will be available at build time.
Finally, your root directory should now include these new files:
Directorysrc/
- env.d.ts
Directorylib/
- contentful.ts
- .env
- astro.config.mjs
- package.json
Fetching data
Section titled Fetching dataAstro components can fetch data from your Contentful account by using the contentfulClient
and specifying the content_type
.
For example, if you have a “blogPost” content type that has a text field for a title and a rich text field for content, your component might look like this:
---import { contentfulClient } from "../lib/contentful";import { documentToHtmlString } from "@contentful/rich-text-html-renderer";import type { EntryFieldTypes } from "contentful";
interface BlogPost { contentTypeId: "blogPost", fields: { title: EntryFieldTypes.Text content: EntryFieldTypes.RichText, }}
const entries = await contentfulClient.getEntries<BlogPost>({ content_type: "blogPost",});---<body> {entries.items.map((item) => ( <section> <h2>{item.fields.title}</h2> <article set:html={documentToHtmlString(item.fields.content)}></article> </section> ))}</body>