Deploying Next JS on Vercel

6 replies
Last updated: Jan 20, 2021
I’m having some issues with setting up a fresh setup of nextjs + sanity + vercel. I’ve got the build working locally and its able to find the static paths but when i run a deploy through vercel i get the following:
21:00:59.874  	> Build error occurred
21:00:59.876  	Error: Invalid `paths` value returned from getStaticPaths in /[slug].
21:00:59.876  	`paths` must be an array of strings or objects of shape { params: [key: string]: string }
21:00:59.877  	    at buildStaticPaths (/vercel/workpath0/node_modules/next/dist/build/utils.js:16:1065)
21:00:59.877  	    at processTicksAndRejections (internal/process/task_queues.js:97:5)
21:00:59.877  	    at async Object.isPageStatic (/vercel/workpath0/node_modules/next/dist/build/utils.js:26:612) {
21:00:59.877  	  type: 'Error'
21:00:59.877  	}
Would anyone know why this would happen? I’m think it may be some sort of permission thing for vercel to access sanity potentially? Thanks in advance!
AI Update

This is a classic environment variable issue when deploying Next.js + Sanity to Vercel! The error you're seeing happens because getStaticPaths is returning undefined instead of an array, which means your Sanity client isn't successfully fetching data during the Vercel build.

Based on this exact issue from the Sanity community, the problem is almost always incorrect environment variables in Vercel. Here's what's happening:

The Root Cause

Your code works locally because you're using local environment variables (likely in a .env.local file), but when Vercel builds your site, it's using the environment variables configured in your Vercel project settings. If these are incorrect or missing, the Sanity client can't connect to your dataset, returns undefined, and Next.js throws that error.

The Solution

  1. Check your Vercel environment variables: Go to your Vercel project settings → Environment Variables and verify:

    • NEXT_PUBLIC_SANITY_PROJECT_ID matches your actual Sanity project ID
    • NEXT_PUBLIC_SANITY_DATASET is set (usually production)
    • Any other Sanity-related variables are correct
  2. Verify your dataset is public: In your Sanity project settings, make sure your production dataset has public read access enabled (if you're not using authentication).

  3. Add debugging: Temporarily add console.log() statements to see what's happening:

export async function getStaticPaths() {
  const client = getClient()
  console.log('Project ID:', client.config().projectId) // Check this matches
  
  const routes = await client.fetch(`*[_type == "route" && defined(slug.current)]{
    "params": {"slug": slug.current}
  }`)
  
  console.log('Routes fetched:', routes) // This will show in Vercel build logs
  
  return {
    paths: routes || [],  // Use empty array instead of null
    fallback: true
  }
}
  1. Use an empty array as fallback: Change paths: routes || null to paths: routes || [] - Next.js expects an array, and an empty array with fallback: true is valid.

The community member who posted this exact issue solved it by discovering their project ID was incorrect and their Vercel environment variables weren't set up properly. Check those Vercel environment variables first - that's almost certainly where your issue is!

are you talking to the prodution dataset?can you verify it’s set to public?
i would also just console.log(paths) to see whats happening
it is set to public, I’ll also console log paths
export async function getStaticProps ({ params = {}, preview = false }) {
  const { slug } = params
  const { page: pageData } = await getClient(preview).fetch(query, {
    slug
  })

  return {
    props: { preview, pageData, slug }
  }
}

export async function getStaticPaths () {
  const routes = await getClient()
    .fetch(`*[_type == "route" && defined(slug.current)]{
    "params": {"slug": slug.current}
  }`)

  return {
    paths: routes || null,
    fallback: true
  }
}
Heres the function in question also with its matching static props
After console logging
routes
just before the return its currently just undefined
Figured it out, after console logging the getClient I noticed by project ID wasn’t correct but the reason why i didnt have the issue locally was because my environment variables setup in vercel were wrong so when they we’re being used thats when i ran into this issue

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.

Was this answer helpful?