rsnext/examples/cms-sanity/sanity.config.ts
Cody Olsen 00e88b82dd
Update Sanity example deps and implementation (#65744)
- Uses the new `presentationTool.resolve.locations` instead of
`presentationTool.locate` API, which doesn't require wrangling `rxjs`.
- Sets up the new `presentationTool.resolve.mainDocuments` API, which
automatically opens up the post you're previewing on the left side in
the editor on the right side.
- Removes the `sanity-typegen.json` config file as `sanity typegen` now
looks for top level `app` and `sanity` folders by default.
- Enables `^` semver ranges for deps again now that our turbopack
support is stable, so that we only have to send you PRs when bumping
majors or changing the implementation itself.
- Sets up `sanity.config.ts` so it's possible to use `npx sanity dev`
for quickly iterating on schemas.
2024-05-16 16:51:49 -07:00

89 lines
2.7 KiB
TypeScript

"use client";
/**
* This config is used to set up Sanity Studio that's mounted on the `app/(sanity)/studio/[[...tool]]/page.tsx` route
*/
import { visionTool } from "@sanity/vision";
import { PluginOptions, defineConfig } from "sanity";
import { unsplashImageAsset } from "sanity-plugin-asset-source-unsplash";
import {
presentationTool,
defineDocuments,
defineLocations,
type DocumentLocation,
} from "sanity/presentation";
import { structureTool } from "sanity/structure";
import { apiVersion, dataset, projectId, studioUrl } from "@/sanity/lib/api";
import { pageStructure, singletonPlugin } from "@/sanity/plugins/settings";
import { assistWithPresets } from "@/sanity/plugins/assist";
import author from "@/sanity/schemas/documents/author";
import post from "@/sanity/schemas/documents/post";
import settings from "@/sanity/schemas/singletons/settings";
import { resolveHref } from "@/sanity/lib/utils";
const homeLocation = {
title: "Home",
href: "/",
} satisfies DocumentLocation;
export default defineConfig({
basePath: studioUrl,
projectId,
dataset,
schema: {
types: [
// Singletons
settings,
// Documents
post,
author,
],
},
plugins: [
presentationTool({
resolve: {
mainDocuments: defineDocuments([
{
route: "/posts/:slug",
filter: `_type == "post" && slug.current == $slug`,
},
]),
locations: {
settings: defineLocations({
locations: [homeLocation],
message: "This document is used on all pages",
tone: "caution",
}),
post: defineLocations({
select: {
title: "title",
slug: "slug.current",
},
resolve: (doc) => ({
locations: [
{
title: doc?.title || "Untitled",
href: resolveHref("post", doc?.slug)!,
},
homeLocation,
],
}),
}),
},
},
previewUrl: { previewMode: { enable: "/api/draft" } },
}),
structureTool({ structure: pageStructure([settings]) }),
// Configures the global "new document" button, and document actions, to suit the Settings document singleton
singletonPlugin([settings.name]),
// Add an image asset source for Unsplash
unsplashImageAsset(),
// Sets up AI Assist with preset prompts
// https://www.sanity.io/docs/ai-assist
assistWithPresets(),
// Vision lets you query your content with GROQ in the studio
// https://www.sanity.io/docs/the-vision-plugin
process.env.NODE_ENV === "development" &&
visionTool({ defaultApiVersion: apiVersion }),
].filter(Boolean) as PluginOptions[],
});