test blog 1

La Ruche Studio
test blog 1

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:

npmpnpmyarnbun

# outside your studio directorynpx create-next-app@latest nextjs-hello-world --tailwind --ts --app --src-dir --eslint --import-alias @/* --turbopackcd nextjs-hello-world

├─ /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

npmpnpmyarnbun

# in nextjs-hello-worldnpm install --legacy-peer-deps next-sanity @sanity/image-url

3Start the development server

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

npmpnpmyarnbun

# in nextjs-hello-worldnpm run dev

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:

/nextjs-hello-world/src/sanity/client.ts

import { createClient } from "next-sanity";export const client = createClient({ projectId: "YOUR-PROJECT-ID", dataset: "production", apiVersion: "2024-01-01", useCdn: false,});

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.

/nextjs-hello-world/src/app/page.tsx

import Link from "next/link";import { type SanityDocument } from "next-sanity";import { client } from "@/sanity/client";const POSTS_QUERY = `*[ _type == "post" && defined(slug.current)]|order(publishedAt desc)[0...12]{_id, title, slug, publishedAt}`;const options = { next: { revalidate: 30 } };export default async function IndexPage() { const posts = await client.fetch<SanityDocument[]>(POSTS_QUERY, {}, options); return ( <main className="container mx-auto min-h-screen max-w-3xl p-8"> <h1 className="text-4xl font-bold mb-8">Posts</h1> <ul className="flex flex-col gap-y-4"> {posts.map((post) => ( <li className="hover:underline" key={post._id}> <Link href={`/${post.slug.current}`}> <h2 className="text-xl font-semibold">{post.title}</h2> <p>{new Date(post.publishedAt).toLocaleDateString()}</p> </Link> </li> ))} </ul> </main> );}

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