Understanding next-sanity, the all-in-one Sanity toolkit for production-grade content-editable Next.js applications.
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_
.
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.
.env.local
file at the root of your application.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
sanity init
again with the --env
flagnpx 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.
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 datasetsanity.config.ts
is used to configure the Sanity Studio, including schema types, plugins, and more.
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.
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.