How do I Deploy Sanity and Next.js (to Vercel) when they Live in a Top Level Folder Side by Side?

13 replies
Last updated: Oct 1, 2021
Hello thereHow do I deploy Sanity & NextJS (to Vercel) when they live in a top level folder side by side? ie

- my-project
-- studio
-- web
AI Update

When deploying a monorepo with Sanity Studio and Next.js in separate folders to Vercel, you'll need to configure each project individually. Here's how to handle your structure:

Deploying the Structure

For your setup:

my-project/
  ├── studio/
  └── web/

You'll create two separate Vercel projects, each pointing to the same GitHub repository.

Studio Deployment

  1. Set Root Directory: In Vercel project settings, set the Root Directory to studio

  2. Add vercel.json in your studio folder:

    {
      "version": 2,
      "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
    }

    This configures the SPA fallback so all routes work correctly.

  3. Update package.json build script:

    {
      "scripts": {
        "build": "sanity build"
      }
    }
  4. Install Sanity CLI as dependency: Add @sanity/cli to your package.json devDependencies (not globally). This was the key fix that solved deployment issues.

  5. Output Directory: Set to dist (Sanity's default build output)

  6. Add CORS: After deploying, run:

    sanity cors add https://your-studio-url.vercel.app --credentials

Next.js (Web) Deployment

  1. Set Root Directory: In a separate Vercel project, set Root Directory to web
  2. Vercel will auto-detect Next.js and configure build settings
  3. Add your Sanity project ID and dataset as environment variables

Important Notes

  • Two Vercel Projects: You need separate Vercel projects for studio and web, both connected to the same repo but with different root directories
  • Build Command Location: Make sure dependencies are installed in the correct subfolder, not at the monorepo root
  • The Turbo Start Sanity template demonstrates this exact pattern with a working monorepo structure

This approach works well for learning and smaller projects. For production monorepos, consider using tools like Turborepo for better dependency management and build caching.

In the past I’ve deployed a solution where Sanity lived inside a NextJS app, that was easy enough.
I’m now dealing with Sanity and NextJS living at the same level inside a top level folder like on https://github.com/SimeonGriggs/sanity-nextjs-preview
do I need to have two repositories living inside a third one?
do I deploy the top level one or Sanity and NextJS individually?
is there any documentation/blog/case study I can reference to?
deploy your next app from the web folder, and the studio from the studio folder
in vercel, you need to specify that the
web
folder is your project root.
for sanity, navigate to your studio folder and run
sanity deploy
hi
user U
thanks for getting back to me
does it mean I need to have two separate repos in github then?
nope, you can have a single repo 🙂
github.com/selbekk/selbekk-io is my website, and a good example of doing stuff this way 🙂
that’s awesome 👍 thanks for sharing, that’s insightful 🤓
🙂 you're welcome!

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?