Strapi & Astro
本頁內容尚未翻譯。
Strapi is an open-source, customizable, headless CMS.
Integrating with Astro
Section titled Integrating with AstroThis guide will build a wrapper function to connect Strapi with Astro.
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 Strapi CMS server - You can set up a Strapi server on a local environment.
Adding the Strapi URL in .env
Section titled Adding the Strapi URL in .envTo add your Strapi URL to Astro, create a .env
file in the root of your project (if one does not already exist) and add the following variable:
Restart the dev server to use this environment variable in your Astro project.
If you would like to have IntelliSense for your environment variable, you can create a env.d.ts
file in the src/
directory and configure ImportMetaEnv
like this:
Your root directory should now include the new file(s):
目錄src/
- env.d.ts
- .env
- astro.config.mjs
- package.json
Creating the API wrapper
Section titled Creating the API wrapperCreate a new file at src/lib/strapi.ts
and add the following wrapper function to interact with the Strapi API:
This function requires an object with the following properties:
endpoint
- The endpoint you are fetching.query
- The query parameters to add to the end of URLwrappedByKey
- Thedata
key in the object that wraps yourResponse
.wrappedByList
- A parameter to “unwrap” the list returned by Strapi, and return only the first item.
Optional: Creating the Article interface
Section titled Optional: Creating the Article interfaceIf you are using TypeScript, create the following Article interface to correspond to the Strapi content types at src/interfaces/article.ts
:
You can modify this interface, or create multiple interfaces, to correspond to your own project data.
目錄src/
目錄interfaces/
- article.ts
目錄lib/
- strapi.ts
- env.d.ts
- .env
- astro.config.mjs
- package.json
Displaying a list of articles
Section titled Displaying a list of articles-
Update your home page
src/pages/index.astro
to display a list of blog posts, each with a description and a link to its own page. -
Import the wrapper function and the interface. Add the following API call to fetch your articles and return a list:
The API call requests data from
http://localhost:1337/api/articles
and returnsarticles
: an array of json objects representing your data: -
Using data from the
articles
array returned by the API, display your Strapi blog posts in a list. These posts will link to their own individual pages, which you will create in the next step.
Generating article pages
Section titled Generating article pagesCreate the file src/pages/blog/[slug].astro
to dynamically generate a page for each article.
目錄src/
目錄interfaces/
- article.ts
目錄lib/
- strapi.ts
目錄pages/
- index.astro
目錄blog/
- [slug].astro
- env.d.ts
- .env
- astro.config.mjs
- package.json
Static site generation
Section titled Static site generationIn Astro’s default static mode (SSG), use getStaticPaths()
to fetch your list of articles from Strapi.
Create the template for each page using the properties of each post object.
Make sure to choose the right rendering for your content. For markdown check out our markdown guide. If you are rendering html refer to this guide for safety.
Server-side rendering
Section titled Server-side renderingIf you’ve opted into on-demand rendering, generate your dynamic routes using the following code:
Create the src/pages/blog/[slug].astro
file:
This file will fetch and render the page data from Strapi that matches the dynamic slug
parameter.
Since you are using a redirect to /404
, create a 404 page in src/pages
:
If the article is not found, the user will be redirected to this 404 page and be greeted by a lovely cat.
Publishing your site
Section titled Publishing your siteTo deploy your website, visit our deployment guides and follow the instructions for your preferred hosting provider.
Rebuild on changes
Section titled Rebuild on changesIf your project is using Astro’s default static mode, you will need to set up a webhook to trigger a new build when your content changes. If you are using Netlify or Vercel as your hosting provider, you can use its webhook feature to trigger a new build from Strapi.
Netlify
Section titled NetlifyTo set up a webhook in Netlify:
-
Go to your site dashboard and click on Build & deploy.
-
Under the Continuous Deployment tab, find the Build hooks section and click on Add build hook.
-
Provide a name for your webhook and select the branch you want to trigger the build on. Click on Save and copy the generated URL.
Vercel
Section titled VercelTo set up a webhook in Vercel:
-
Go to your project dashboard and click on Settings.
-
Under the Git tab, find the Deploy Hooks section.
-
Provide a name for your webhook and the branch you want to trigger the build on. Click Add and copy the generated URL.
Adding a webhook to Strapi
Section titled Adding a webhook to StrapiFollow the Strapi webhooks guide to create a webhook in your Strapi admin panel.
Official Resources
Section titled Official Resources- Strapi Blog Guide For React by Strapi