Astro quickstart

Displaying content in an Astro front end

You’ve configured your Studio with a post document type and learned how to query from your hosted dataset. Before deploying the Studio, let’s query and display this content on the front-end framework of your choice.

1Install a new Astro application

If you have an existing application, skip this first step and adapt the rest of the lesson to install Sanity dependencies to fetch and render content.

Run the following in a new tab or window in your Terminal (keep the Studio running) to create a new Astro application with Tailwind CSS and TypeScript.

You should now have your Studio and Astro application in two separate, adjacent folders:

2Install Sanity dependencies

Run the following inside the astro-hello-world directory to:

3Add Types for Sanity Client

Update src/env.d.ts with the following additional code for TypeScript support of Sanity Client.

4Configure the Sanity client

Update the integration configuration to configure a Sanity Client to fetch content.

5Start the development server

Run the following command and open http://localhost:4321 in your browser.

6Display content on a posts index page

Astro performs data fetching inside front-matter blocks (---) at the top of .astro files

Create a route for a page with a list of posts fetched from your Sanity dataset, and visit http://localhost:4321/posts

7Display individual posts

Create a new route for individual post pages.

The dynamic value of a slug when visiting /posts/[slug] in the URL is used as a parameter in the GROQ query used by Sanity Client.

Notice that we’re using Tailwind CSS Typography’s prose class name to style the post’s body block content. Install it in your project following their documentation.

Was this page helpful?