CoursesContent-driven web application foundationsThe next-sanity toolkit
Track
Work-ready Next.js

Content-driven web application foundations

Log in to watch a video walkthrough of this lesson
Video thumbnail

Understanding next-sanity, the all-in-one Sanity toolkit for production-grade content-editable Next.js applications.

Log in to mark your progress for each Lesson and Task

One of the dependencies automatically installed during sanity init in the last lesson was next-sanity, a collection of utilities and conventions for data fetching, Visual Editing, and more. You could look through the readme for full details on what it provides.

For now, let's examine some of the files that were automatically created in the previous lesson and explain their purpose.

A .env.local file should have been created with your Sanity project ID and dataset name. These are not considered sensitive, and so are appended with NEXT_PUBLIC_.

See the Next.js documentation about public and private environment variables.

In future lessons, you'll add secrets and tokens to this file. It is important that you do not check this file in your Git repository. Also, remember that values in this file will need to be recreated when deploying the application to hosting. We'll remind you of this when we get there.

Confirm you have an .env.local file at the root of your application.
.env.local
NEXT_PUBLIC_SANITY_PROJECT_ID="your-project-id"
NEXT_PUBLIC_SANITY_DATASET="production"

Additionally, a file to retrieve, export, and confirm these values exist has been written to src/sanity/env.ts

You can use Sanity CLI to update these values with a new or existing Sanity project by running sanity init again with the --env flag
npx sanity@latest init --env

The file src/sanity/lib/client.ts contains a lightly configured instance of Sanity Client.

Sanity Client is a JavaScript library commonly used to interact with Sanity projects. Its most basic function is querying content, but once authenticated with a token, it can interact with almost every part of a Sanity project.

See more about what Sanity Client can do

You won't need to change the Sanity Client configuration now, but it is good to know where to make modifications later.

The two root files sanity.cli.ts and sanity.config.ts are important for interacting with your project:

  • sanity.cli.ts allows you to run CLI commands that affect the project while targeting the correct project ID and dataset
  • sanity.config.ts is used to configure the Sanity Studio, including schema types, plugins, and more.
Confirm you have these two files at the root of your application.
Run the following command to show project details:
npx sanity@latest debug

In the src/sanity/schemaTypes folder are files for the three document types and one custom type which you can see in the Studio.

You're able to create category, post and author type documents because these have been registered to the Studio configuration.

Datasets are schemaless, so data of any shape could be written into a dataset. But these are the only types made available in the Studio for now. In future lessons, you'll change these schema types, but they give us enough to work with now.

See Improving the editorial experience in Day One with Sanity Studio to see how basic schema type configurations can be dramatically improved.

You now have a Next.js application with an embedded Sanity Studio for creating and publishing content. It's time to start integrating them.

Writing GROQ queries is the most common method of querying content from Sanity. In the next lesson, we'll set up conventions for this.

Courses in the "Work-ready Next.js" track