Builder.io & Astro
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
Builder.io is a visual CMS that supports drag-and-drop content editing for building websites.
This recipe will show you how to connect your Builder space to Astro with zero client-side JavaScript.
Prerequisites
Section titled PrerequisitesTo get started, you will need to have the following:
- A Builder account and space - If you don’t have an account yet, sign up for free and create a new space. If you already have a space with Builder, feel free to use it, but you will need to modify the code to match the model name (
blogpost
) and custom data fields. - A Builder API key - This public key will be used to fetch your content from Builder. Read Builder’s guide on how to find your key.
Setting up credentials
Section titled Setting up credentialsTo add your Builder API key and your Builder model name to Astro, create a .env
file in the root of your project (if one does not already exist) and add the following variables:
BUILDER_API_PUBLIC_KEY=YOUR_API_KEYBUILDER_BLOGPOST_MODEL='blogpost'
Now, you should be able to use this API key in your project.
At the time of writing, this key is public, so you don’t have to worry about hiding or encrypting it.
If you would like to have IntelliSense for your environment variables, you can create a env.d.ts
file in the src/
directory and configure ImportMetaEnv
like this:
interface ImportMetaEnv { readonly BUILDER_API_PUBLIC_KEY: string;}
Your project should now include these files:
Directorysrc/
- env.d.ts
- .env
- astro.config.mjs
- package.json
Making a blog with Astro and Builder
Section titled Making a blog with Astro and BuilderCreating a model for a blog post
Section titled Creating a model for a blog postThe instructions below create an Astro blog using a Builder model (Type: “Section”) called blogpost
that contains two required text fields: title
and slug
.
You can find videos showing this procedure in one of Builder’s official tutorials.
In the Builder app create the model that will represent a blog post: go to the Models tab and click the + Create Model button to create model with the following fields and values:
- Type: Section
- Name: “blogpost”
- Description: “This model is for a blog post”
In your new model use the + New Custom Field button to create 2 new fields:
-
Text field
- Name: “title”
- Required: Yes
- Default value “I forgot to give this a title”
(leave the other parameters as their defaults)
-
Text field
- Name: “slug”
- Required: Yes
- Default value “some-slugs-take-their-time”
(leave the other parameters as their defaults)
Then click the Save button in the upper right.
There are some pitfalls with the slug
field:
-
Make sure your slug is not just a number. This seems to break the fetch request to Builder’s API.
-
Make sure your slugs are unique, since your site’s routing will depend on that.
Setting up the preview
Section titled Setting up the previewTo use Builder’s visual editor, create the page src/pages/builder-preview.astro
that will render the special <builder-component>
:
Directorysrc/
Directorypages/
- builder-preview.astro
- env.d.ts
- .env
- astro.config.mjs
- package.json
Then add the following content:
---const builderAPIpublicKey = import.meta.env.BUILDER_API_PUBLIC_KEY;const builderModel = import.meta.env.BUILDER_BLOGPOST_MODEL;---
<html lang="en"> <head> <title>Preview for builder.io</title> </head> <body> <header>This is your header</header>
<builder-component model={builderModel} api-key={builderAPIpublicKey} ></builder-component> <script async src="https://cdn.builder.io/js/webcomponents"></script>
<footer>This is your footer</footer> </body></html>