Events
-
{events.map((event) => (
-
{event?.name}
{event?.date && ({new Date(event.date).toLocaleDateString()}
)}
))}
# Display content in Next.js https://www.sanity.io/learn/course/day-one-with-sanity-studio/bringing-content-to-a-next-js-front-end.md You've now crafted a content creation experience and learned how to query from the Content Lake. All that's left to do is distribute that content to the world. In this lesson, you'll create a new Next.js application and query for Sanity content. Next.js is a React framework for building full-stack web applications. You'll use it in this lesson because of how simply you can get started but the concepts here could work with any framework or front end library. 1. Prefer alternatives to Next.js? [See "clean starter" templates](https://www.sanity.io/templates) available for Astro, React Router and more ## Install a new Next.js application The command below installs a predefined bare bones template with some sensible defaults, and Tailwind CSS is installed. 1. **Run** the following command at the root `day-one` directory to create a new Next.js application. ```sh # in the root /day-one directory pnpm dlx create-next-app@latest apps/web --tailwind --ts --app --src-dir --eslint --import-alias "@/*" --turbopack --use-pnpm cd apps/web ``` The included flags set some opinionated defaults—such as Tailwind CSS, TypeScript, and eslint—so that you don't have to decide. You should now have your Sanity Studio and Next.js app in two separate, adjacent folders: ```text day-one/ └── apps/ ├── studio/ -> Sanity Studio └── web/ -> Next.js app ``` ### Sanity dependencies 1. **Run** the following command to install Sanity dependencies inside the `apps/web` directory ```sh # inside the apps/web directory pnpm install next-sanity ``` * [`next-sanity`](https://github.com/sanity-io/next-sanity) is a collection of utilities specifically tuned for Next.js when integrating with Sanity * [`@portabletext/react`](https://github.com/portabletext/react-portabletext#readme) (installed as part of `next-sanity`) is a React Component for rendering Portable Text with default components and the option to extend them for your own block content. This lesson has a deliberately narrow focus to query and render Sanity content in a front-end. For projects going into production, there are more factors to consider such as caching, revalidation, visual editing, SEO and more. As well as extra editing affordances such as "page building". 1. Consider taking the track of courses to explore in more detail. 2. The [`next-sanity` documentation](https://github.com/sanity-io/next-sanity?tab=readme-ov-file#next-sanity) contains more details for preparing Sanity and Next.js for production. 1. **Run** the following to start the development server for both your Studio and Next.js apps ```sh pnpm run dev ``` 1. Open [http://localhost:3000](http://localhost:3000) in your browser You should now see the default page for new Next.js applications, just like this:  ## Fetching Sanity content To fetch content from Sanity, you'll need a configured Sanity Client. In the code snippet below, you'll need to modify the `projectId` value to the one in your Studio's `sanity.config.ts` 1. **Create** a new file for the Sanity Client with your `projectId` ```typescript:apps/web/src/sanity/client.ts import { createClient } from "next-sanity"; export const client = createClient({ projectId: "REPLACE_WITH_YOUR_PROJECT_ID", dataset: "production", apiVersion: "2025-07-09", useCdn: false, }); ``` 1. In a production project, sharing values such as your Project ID across configuration files in a monorepo is better done with either a global environment variable or a `/packages` directory in your workspace. Sanity Client is a convenient way to interact with almost all of Sanity's APIs, such as fetching for content. In many frameworks, Sanity Client is all you'd use. In Next.js it's simple to do much better than a simple fetch. Instead of only making fetches as a page is requested, let's make the application ["Live by Default"](https://www.sanity.io/live) with a little extra configuration. 1. **Create** a new file to create live fetching utilities ```typescript:apps/web/src/sanity/live.ts import { defineLive } from "next-sanity/live"; import { client } from "@/sanity/client"; export const { sanityFetch, SanityLive } = defineLive({ client }); ``` 1. Next.js uses a "path alias" to import files from the `/src` directory using `@/` as shorthand. So you're importing `client` from `src/sanity/client.ts` in the example above. Not to be confused with the package `@sanity/client`! 1. **Update** the root layout to include the `SanityLive` component ```tsx:src/app/layout.tsx import { SanityLive } from "@/sanity/live"; import "./globals.css"; export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return (
{children}{new Date(event.date).toLocaleDateString()}
)}