Back
Building an Event App with Astro & Prismic
Let's learn how to build an event manager app with Astro and Prismic headless CMS. This tutorial is easy to follow and will help you add to your portfolio.
Previously, traditional CMS provided us with the ability to have all our content, such as images, videos, templates, and custom code, in a single environment. However, as the web grew and became more complex, the need to scale increased, and the past approach was no longer adequate. The introduction of the headless CMS brought about how our content serves as data over an API. A headless CMS makes it easy to distribute data across different platforms and devices on the internet.
In this article, we’ll talk about Prismic and learn how to set it up with Astro. In addition, we’ll learn to source data from Prismic to a frontend – Astro. Also, we’ll build an event manager app with Astro and a Prismic headless CMS.
What Is Prismic?
Prismic is a CMS backend for websites and apps that’s designed to increase developer productivity and includes a visual builder for creating posts and pages. Prismic is specifically a headless, API-based, hosted, proprietary CMS for technical independence that allows developers to use their preferred framework & programming language.
Major companies, such as Spotify, DigitalOcean, and Netflix, use Prismic as their CMS. Prismic allows users to create websites and apps with instantly updatable content and custom designs.
The Benefits of Using Prismic
Prismic allows developers to use nearly any programming language or framework to build websites and apps. It can be used with React, Vue, Angular, Rails, Node and Laravel.
Prismic allows users to create branded page sections and components that are reusable.
It provides a simple, feature-rich admin panel that allows content managers to update content on websites and apps instantly and independently.
Prismic allows content creators and developers to focus on creating content, managing resources, and developing a more efficient and beautiful user interface. Meanwhile, the Prismic team maintains its content infrastructure in the cloud.
Thanks to their partnership with Imgix, Primsic allows users to customize images. They also make it easy to create and schedule releases, as well as pull content from third parties into integration fields.
What Is Astro?
Astro is a web framework designed for building fast, content-rich websites. Astro lets us develop faster websites with less client-side Javascript. It leverages server-side rendering over client-side rendering and supports code splitting, file-based routing, asset handling, build process, bundling, optimization and data fetching. Astro has built-in support for popular web frameworks and tools such as React, Solid, Vue, Typescript, Markdown and more.
Also, Astro integrates partial hydration into websites and applications, which increases site performance.
To learn more about Astro, see the article here.
Setting up a Prismic Headless CMS
To set up Prismic, we first need to create an account. After creating an account, we’ll be directed to the dashboard. Next, we click on “create a new repository with another framework.”
After creating a new repository, we’ll be directed to the admin dashboard, where we’ll create and define our Custom Types. In this case, we want to create Events under Documents, so we click on Custom Types. We’ll start by creating our Events custom type. Fill out the form as shown below:
Once this is created, we’ll be directed to a new page where we’ll define our custom type.
Next, we’ll be using the drag-and-drop feature that Prismic offers to create our custom type. We use the drag-and-drop editor to fill out the following:
A
title
field with the value title - the title of the event.A
rich text
field with the value description - the description of the event.A
date
field with the value date - the date of the event.A
number
field with the value duration - the time duration of the event.A
number
field with the value price - the price to attend events.An
image
field with the value image - the event’s featured image.
After defining custom types, click on save.
Adding Contents
Next, let’s add some content to our custom types. On our admin dashboard, we first select Documents and click on Create New. We’ll be prompted to create an entry, but start by adding content for the Events:
We also added the tags tech-hangout and live-coding as shown in the image below. We will be using the tags to filter our Events.
Here, we can fill out our desired information in the fields created earlier. We can add as many Events as desired by clicking on Create New. After filling out the entry, we then click Save & Publish.
Setting up Roles & Permissions
Before we can access our data through the API endpoint, we need to grant access to our API by navigating to Settings on our admin dashboard and clicking on API & Security. We’ll see an API endpoint automatically generated by Primsic. Next, we copy and save this API endpoint. Finally, we try accessing https://your-repo-name.cdn.prismic.io/api/v2
to see all of our content returned in our browser.
In order to use the Prismic preview feature, we have to make the API access token private.
Setting up Our Frontend with Astro
In this section, we’ll install and set up Astro, and build the event app user interface. So let’s get started by installing Astro. In our terminal, we run the following command:
The above command will bootstrap the application configuration for the project. We’ll be asked to make some choices based on how we want the project to run on Astro. Our selection should look like the one in the image below:
After selecting the configuration for the project, we change it to the project directory. Then, we’ll start the application with the command below:
As we look through the Astro folder structure, we’ll notice that every Astro component makes use of the
.astro
extension. It also allows us to integrate any web framework. If we're using VS Code, we can add IntelliSense support for it by installing this extension.
Configuring Our Dependencies
Before we start developing our application, let's install and configure some dependencies:
Tailwind – a utility-first CSS framework for styling our application.
@prismicio/client
– a package responsible for handling requests to our Prismic endpoint. It is also used in creating web apps with Prismic and Javascript.@prismicio/helpers
– a package that helps us work with data from Prismic.
Let’s start by installing and configuring Tailwind into our project by running this command:
We’ll see the Astro CLI add Tailwind and React dependencies to our project. Next, we’ll go to astro.config.mjs
, where we’ll see that Astro has automatically included these codes into our project. Astro allows us to integrate with any frontend framework.
After installing Tailwind and React into our project, we’ll install libraries that will help us fetch data from Prismic into our project. In our terminal, we run the command below to install the dependencies:
Connecting Prismic to Astro
Next, in our src
folder, we create a prismic.js
file and add the code below:
First, we imported Prismic
from @prismicio/client
then initialized our API endpoint, and passed it to prismic.createClient()
to generate a client object. The client object contains a collection of properties and methods to help us query the Prismic API. With this done, we can start writing our query.
Adding a Query to Our Prismic Client
Inside our prismic.js
file, we’ll add the code below:
We’ll use methods to write our query that are provided by the client object. The getAllEvents
query will help us request our Events, and the getAllCategories
will filter our query request.
Before we start building our layouts, let’s create a .env
in our root folder and paste it into our Prismic URL API:
Building Our Application UI with Astro
We’ll start by creating our app layout. The layout of Astro allows us to create reusable components. We’ll create a navigation layout for all the pages in our application. First, replace the content inside layouts/layout.astro
in the src folder with the code snippet below:
Astro allows JavaScript and TypeScript to be written inside the ---
block. The area is called the Component Script. Astro also uses Astro.props
to pass data to components in Astro and make it available to use throughout the component. Below the ---
block is where we structure the UI. Astro allows us to insert child elements from other files into the component template by using the element as a placeholder for external HTML information.
Displaying Our Events
In this section, we’ll request Prismic. Once we get back our desired data, we’ll populate the home page with the different events received from Prismic. However, before doing this, we need to create a card component that we can reuse to display our event data.
In the components folder, we’ll create the Card
component, and add the following:
Here, we have passed an event
prop into this component. The prop is an array of events passed down from the home page pages/index.astro
. We added styles using TailwindCSS, and added the necessary information to be displayed on the card, thanks to the prop. We also noticed some kind of dynamic routing for when we click on an event:
Now that we’ve created our components, let’s replace the content in the pages/index.astro
file with the code snippet below:
Each file with the .astro
extension in the src/pages
folder will be a page on our site due to Astro’s routing mechanism called “file-based routing.”
Displaying a Particular Event
In this section, we’ll be displaying a particular Event. In our pages
folder, we create an events/[id].astro
file:
Using the integrated getStaticPaths()
function, Astro enables us to execute dynamic routing. We can render the pages specified in params, which is our id
, using getStaticPaths()
. To be able to render the Rich Text and Title fields as React components, we need to install and use the component from @prismicio/react
. The PrismicRichText
component can be customized to our taste.
Filtering Events
In this section, we’ll filter our Events based on the tags. We want each tag to be displayed on its page. First, we create a techHangout.astro
and liveCoding.astro
file in the src/pages
folder. Next, we add the code below to our liveCoding.astro
file:
We have two tags for our events: tech-hangout
and live-coding
. We want each tag to be displayed on its page. From our query, we filter events based on each tag, and display them on their respective pages. After we do the same for the techHangout
event page, everything should work fine.
Conclusion
In this article, we demonstrated how to set up Prismic, and fetch data from Prismic using Astro and RESTful API. In the process, we created an event custom type to build the application. We also looked at how to write queries using the Prismic client object to help query the Prismic API. Lastly, we created a page that displays information about each event, and we filtered the events based on their tags to enable easy navigation.
All code for this article can be found on GitHub.