Skip to content

Sanity deployment to Vercel failing with multiple errors

15repliesLast updated: Nov 13, 2026

Hallo folks! I am just getting started on Sanity and I REALLY like it so far! And it's working great in my local environment, but when I'm trying to deploy to Vercel I'm running into a whole heap of issues. I'm trying to follow along this tutorial (which may well be the issue) with some deviations (bc of course): https://www.sanity.io/blog/build-your-own-blog-with-sanity-and-next-js . Anyways, when I try to deploy to Vercel I'm getting a ton of errors and it's gotten to be a bit frustrating! Help would be SUPER appreciated (errors in a reply).
AI Update

A build that works locally but fails on Vercel usually means the page reads data that is undefined at build time. Common reasons: the Sanity project ID or dataset isn't set in Vercel, the posts aren't published, or the code doesn't handle a null result. A few setup issues come up often too: Node version, the build script, the root directory and CORS origins.

1. Set the environment variables in Vercel

Vercel doesn't read your local .env.local. In the Vercel project go to Settings → Environment Variables and add every value from .env.local, for example NEXT_PUBLIC_SANITY_PROJECT_ID and NEXT_PUBLIC_SANITY_DATASET. Then redeploy (Deploy to Vercel). Your project ID is in sanity.config.ts, or run npx sanity manage from the Studio folder.

You can also use Sanity's Vercel integration. It adds NEXT_PUBLIC_SANITY_PROJECT_ID, NEXT_PUBLIC_SANITY_DATASET and read/write token variables to the Vercel project for you. Keep the tokens server-side only.

2. Make sure the data exists and guard against null

const post = await client.fetch(POST_QUERY, { slug });
// ...
<h1>{post?.title}</h1>

3. Use the current tutorial code

The tutorial was updated on March 16, 2026. It now uses the App Router with server components and next-sanity, which includes the client, PortableText and defineQuery in one package. If your code imports groq and @portabletext/react separately and uses the Pages Router, it follows the older version. The current client setup:

npm install next-sanity @sanity/image-url
// src/sanity/client.ts
import { createClient } from "next-sanity";

export const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
  apiVersion: "2025-05-01",
  useCdn: false,
});

4. Check the build settings

"engines": {
  "node": ">=22.12.0"
}

5. Add CORS origins where needed

Server-side fetches (server components, SSR, build time) don't need a CORS origin. You need one when the browser requests Sanity content directly, or when a Studio is hosted on your Vercel domain. Add your production URL, for example https://your-project.vercel.app, in either of two ways (CORS docs):

npx sanity cors add https://your-project.vercel.app

Turn on Allow credentials only for origins that host a Studio or send authenticated requests. Don't add a credentialed https://*.vercel.app wildcard. For preview deployments, scope the wildcard to your team slug instead, for example https://*-my-team.vercel.app.

If it still fails, open the Vercel Build Logs and find the first error. That's usually the real cause, and the errors after it are side effects.

Show original thread
15 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions