Next.js quickstart

Displaying content in Next.js

1Install a new Next.js 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 Next.js application with Tailwind CSS and TypeScript.

You should now have your Studio and Next.js application in two separate, adjacent folders:

├─ /nextjs-hello-world
└─ /studio-hello-world

2Install Sanity dependencies

Run the following inside the nextjs-hello-world directory to install:

  • next-sanity a collection of utilities for integrating Next.js with Sanity
  • @sanity/image-url helper functions to take image data from Sanity and create a URL

3Start the development server

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

4Configure the Sanity client

To fetch content from Sanity, you’ll first need to configure a Sanity Client.

Create a directory nextjs-hello-world/src/sanity and within it create a client.ts file, with the following code:

5Display content on the homepage

Next.js uses server components for loading data at specific routes. The current home page can be found at src/app/page.tsx.

Update it to render a list of posts fetched from your Sanity dataset using the code below.

6Display individual posts

Create a new route for individual post pages.

The dynamic value of a slug when visiting /[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?