Hashnode & Astro
Esta página aún no está disponible en tu idioma.
Hashnode is a hosted CMS that allows you to create a blog or publication.
Integrating with Astro
Section titled Integrating with AstroIn this section, we’ll use the graphql-request project to bring your data into your Astro project.
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 Hashnode site - Its free to get started and create a Solo site on Hashnode. You can create a site by visiting Hashnode.
Installing dependencies
Section titled Installing dependenciesFirst, you will need to install the graphql-request
package. This package is a minimal GraphQL client that works well with Astro.
Making a blog with Astro and Hashnode
Section titled Making a blog with Astro and HashnodeWith the setup above, you are now able to create a blog that uses Hashnode as the CMS.
Prerequisites
Section titled Prerequisites- A Hashnode Blog
- An Astro project integrated with the graphql-request package installed.
This example will create an index page that lists posts with links to dynamically-generated individual post pages.
Fetching Data
Section titled Fetching DataYou can fetch your site’s data with the graphql-request package.
First, create the client.ts
& schema.ts
files under a lib
directory.
Directorysrc/
Directorylib/
- client.ts
- schema.ts
Directorypages/
- index.astro
- astro.config.mjs
- package.json
Initialize an API instance with the GraphQLClient using the URL from your Hashnode Website.
And setup the schema file to define the shape of the data returned from the Hashnode API.
Displaying a list of posts
Section titled Displaying a list of postsThe page src/pages/index.astro
will display a list of posts, each with a description and link to its own page.
Directorysrc/
- …
Directorylib/
- client.ts
- schema.ts
Directorypages/
- index.astro
- astro.config.mjs
- package.json
Import getAllPosts()
in the Astro frontmatter to fetch the data from Hashnode.
Fetching via the graphql client returns an array of objects containing the properties for each post such as:
title
- the title of the postbrief
- the HTML rendering of the content of the postcoverImage.url
- the source URL of the featured image of the postslug
- the slug of the post
Use the posts
array returned from the fetch to display a list of blog posts on the page.
Generating pages
Section titled Generating pagesThe page src/pages/post/[slug].astro
dynamically generates a page for each post.
Directorysrc/
- …
Directorylib/
- client.ts
- schema.ts
Directorypages/
- index.astro
Directorypost/
- [slug].astro
- astro.config.mjs
- package.json
Import getAllPosts()
and getPost()
in the Astro frontmatter to fetch the data from Hashnode and generate the page route for each post.
Create the template for each page using the properties of each post
object.
<Fragment />
is a built-in Astro component which allows you to avoid an unnecessary wrapper element. This can be especially useful when fetching HTML from a CMS (e.g. Hashnode or WordPress).
Publishing your site
Section titled Publishing your siteTo deploy your site visit our deployment guide and follow the instructions for your preferred hosting provider.
Community Resources
Section titled Community Resourcesastro-hashnode
on GitHub