Naalya Handbook
Sanity CMS

Setup

Run the Sanity Studio and wire the frontend @sanity/client, project, dataset, and the env vars that make both go.

There are two Sanity apps in this repo, and they run separately. Get that straight first and everything else clicks into place. The Studio in sanity/ is the editing app: the desk where content authors actually type. The frontend at the repo root is the React Router site, and it reads (and writes) that content through a single @sanity/client. Same Sanity project on the backend; two independent codebases on your machine.

This page gets both running. You'll start the Studio on port 3333, then point the frontend client at the project with a handful of env vars. Nothing here is conceptual heavy-lifting: it's mostly knowing which folder to stand in and which four variables to set.

One project, two clients

Both apps talk to the same Sanity project: id hdefozzv, dataset development. The Studio hardcodes those values; the frontend reads them from env. That asymmetry is the single most common source of "why won't it connect" confusion, so keep it in mind.


Step 1: Run the Studio

The Studio is a self-contained pnpm project that lives in sanity/. It carries its own pnpm-lock.yaml and its own node_modules, which means you install and run it from inside that folder: never from the repo root.

terminal
cd sanity
pnpm install        # installs against sanity/pnpm-lock.yaml
pnpm dev            # runs `sanity dev` -> http://localhost:3333

Open http://localhost:3333 and you're in the editing app. The project id and dataset are baked into sanity.config.ts as literal strings, so the Studio needs no env file to boot: it knows where to connect.

`pnpm --filter` will not find the Studio

pnpm-workspace.yaml has no packages: key, only a build-approvals block, so the sanity/ folder is not a workspace member. Running pnpm --filter <studio> dev from the repo root fails every time. The fix is never a flag; it's cd sanity first.

Day to day you only ever need pnpm dev. The other scripts are there when you need them:

ScriptRunsWhen you reach for it
pnpm devsanity devLocal Studio on 3333: your default
pnpm buildsanity buildProduction build
pnpm deploysanity deployPush the hosted Studio (uses appId)
pnpm typegenschema extract + typegenRegenerate types from the schema

To see live preview, the click-to-edit overlays in the Presentation tool, the Studio isn't enough on its own. Its preview iframe points at http://localhost:5173, which is the frontend. So you'll want both running, which is exactly what Step 2 sets up.


Step 2: Configure the frontend client

The frontend Sanity client is app/lib/sanity-client.ts. The comment in that file says it is read-only and carries no token. Writes go through /api/form-submission and sanity-write-client.server.ts. useCdn is true. Stega encoding is enabled only when isStudioPreview is true (the page is inside the Studio iframe).

app/lib/sanity-client.ts (shape)
const sanityClient = createClient({
  projectId: import.meta.env.VITE_SANITY_PROJECT_ID,
  dataset: import.meta.env.VITE_SANITY_DATASET,
  apiVersion: import.meta.env.VITE_SANITY_API_VERSION,
  useCdn: true,
  stega: { enabled: isStudioPreview /* studio URL and a field filter */ },
});

A missing VITE_SANITY_* value is undefined. The client does not throw at construction. Fetches fail later.

Create your .env

Copy the root .env.example. The website client reads VITE_SANITY_PROJECT_ID, VITE_SANITY_DATASET, and VITE_SANITY_API_VERSION. Form writes go through POST /api/form-submission, which checks SANITY_WRITE_TOKEN on the server (app/routes/api/form-submission.ts). That name has no VITE_ prefix. Do not put a write token in a VITE_ variable. The Studio hardcodes its dataset to development in sanity/sanity.config.ts. Match the frontend dataset to that value unless you have a reason not to.

Run the frontend

With the .env in place, install and start the site from the repo root:

terminal
pnpm install
pnpm dev            # react-router dev --host -> http://localhost:5173

The site comes up on http://localhost:5173, the same URL the Studio's preview iframe expects, so live editing lines up by default. Want the generated-API watcher running alongside? Use pnpm dev:all.

No env validation means silent failures

createClient happily accepts undefined for any unset variable: nothing validates your .env. The symptom is fetches that fail at runtime with no clear cause. If data isn't loading, check the root .env first; a typo'd or missing var is the usual culprit.


Step 3: Use the client

Once the client has its env, you fetch by importing the default export. Anywhere in the app, the same instance is the single source of truth:

app/queries/landing/landing.options.ts
import sanityClient from "@/lib/sanity-client";

const data = await sanityClient.fetch(LANDING_PAGE_QUERY);

That one import covers reads, writes, and image transforms: there is no separate read/CDN client, no @sanity/react-loader, no live-query setup. For images specifically, reach for the helpers rather than building URLs by hand:

app/helpers/optimize-sanity-image.ts
import { optimizeSanityImage } from "@/helpers/optimize-sanity-image";

One client, reused everywhere

The same sanityClient is consumed in four places: the root loader() for site-wide data, TanStack Query options for page data, mutations in sanity-mutations.ts, and @sanity/image-url for images. Freshness comes from the query cache (mostly 24 hours), not live subscriptions. Querying with GROQ covers the per-page module convention.


Deploying: VITE_ vars are build-time

One gotcha that bites on first deploy. Because VITE_-prefixed variables are inlined into the bundle at build time, you can't set them as runtime environment variables on the server. When you build the Docker image (Railway), pass the same VITE_SANITY_* values as build ARGs.

Dockerfile (ARG block)
ARG VITE_SANITY_PROJECT_ID
ARG VITE_SANITY_DATASET
ARG VITE_SANITY_API_VERSION
ARG VITE_SANITY_STUDIO_URL

Changing any of these after a build means rebuilding: there is no runtime override. SANITY_WRITE_TOKEN is the opposite: it has no VITE_ prefix and no build ARG, so set it on the running server's environment.

Ignore the unused ARGs

The Dockerfile also declares VITE_SANITY_DEV_APP_ID, VITE_SANITY_PROD_APP_ID, and VITE_BASE_URL, but nothing in the code references them. They're reserved or dead: don't assume they're wired up, and don't waste time setting them.


Where to go next

You have both apps running and the client connected. From here, learn the content model and how to read it.

On this page