Issue with workspaces in Sanity V3 upgrade

5 replies
Last updated: Jan 11, 2023
Hello! đź‘‹
I've ran and upgrade from Sanity V2 to Sanity V3, although seem to be running into issues with workspaces. I have two workspaces dev and production, although it seems that the desk always defaults to dev, similarly when I change the URL to production, it comes up with an error message "Workspace not found". I've pasted the config below, any idea where I might be going wrong? The dropdown at the top is also not appearing
AI Update

I can see exactly what's wrong with your configuration! The issue is that when you're defining multiple workspaces, you need to pass an array to defineConfig, not multiple separate arguments.

Your current code has:

export default defineConfig({
  // first workspace config
},
{
  // second workspace config
});

But it should be:

export default defineConfig([
  {
    // first workspace config
  },
  {
    // second workspace config
  }
]);

Notice the square brackets [...] wrapping both configuration objects? That's the key difference.

Also, when you have multiple workspaces configured this way, you need to navigate to the correct URL path. Instead of going to http://localhost:3333/desk, you need to go to:

  • http://localhost:3333/dev/desk for your dev workspace
  • http://localhost:3333/production/desk for your production workspace

The basePath you've defined ('/dev' and '/production') becomes part of the URL structure. Once you wrap your configs in an array, the workspace dropdown at the top should also appear, allowing you to switch between workspaces.

Here's your corrected config:

import { defineConfig } from "sanity";
import { deskTool } from 'sanity/desk'
import schemas from './schemas/schema'
import { table } from '@sanity/table';
import { colorInput } from "@sanity/color-input";
import deskStructure from './sidebar'
import { visionTool } from '@sanity/vision'

export default defineConfig([
  {
    title: "XX Dev",
    projectId: "XX",
    dataset: "dev",
    name: "dev",
    basePath: '/dev',
    plugins: [deskTool({structure: deskStructure}),
      visionTool(),
      table(),
      colorInput()],
    // ... rest of your dev config
  },
  {
    title: "XX Prod",
    projectId: "XX",
    dataset: "production",
    name: "production",
    basePath: '/production',
    plugins: [deskTool({structure: deskStructure}),
      visionTool(),
      table(),
      colorInput()],
    // ... rest of your production config
  }
]);

This is a super common mistake when migrating to V3 - you're definitely not alone! The workspaces documentation and defineConfig reference have more details on configuring multiple workspaces if you need additional reference.

// sanity.config.js
import { defineConfig } from "sanity";
import { deskTool } from 'sanity/desk'
import schemas from './schemas/schema'
import { table } from '@sanity/table';
import { colorInput } from "@sanity/color-input";
import deskStructure from './sidebar'
import { visionTool } from '@sanity/vision'

export default defineConfig({
  title: "XX Dev",
  projectId: "XX",
  dataset: "dev",
  name: "dev",
  basePath: '/dev',
  plugins: [deskTool({structure: deskStructure}),
    visionTool(),
    table(),
    colorInput()],
    tools: (prev) => {
      // 👇 Uses environment variables set by Vite in development mode
      if (import.meta.env.DEV) {
        return prev
      }
      return prev.filter((tool) => tool.name !== 'vision')
},
  schema: {
    types: schemas,
  },
  document: {
    newDocumentOptions: (prev, { creationContext }) => {
      if (creationContext.type === 'global') {
        return prev.filter((templateItem) => templateItem.templateId != 'settings')
      }
      return prev
    },
    actions: (prev, { schemaType }) => {
      if (schemaType === 'settings') {
        return prev.filter(({ action }) => !['unpublish', 'delete','duplicate'].includes(action))
      }
      return prev
    },
  },
},
{
  title: "XX Prod",
  projectId: "XX",
  dataset: "production",
  name: "production",
  basePath: '/production',
  plugins: [deskTool({structure: deskStructure}),
    visionTool(),
    table(),
    colorInput()],
    tools: (prev) => {
      // 👇 Uses environment variables set by Vite in development mode
      if (import.meta.env.PRODUCTION) {
        return prev
      }
      return prev.filter((tool) => tool.name !== 'vision')
},
  schema: {
    types: schemas,
  },
  document: {
    newDocumentOptions: (prev, { creationContext }) => {
      if (creationContext.type === 'global') {
        return prev.filter((templateItem) => templateItem.templateId != 'settings')
      }
      return prev
    },
    actions: (prev, { schemaType }) => {
      if (schemaType === 'settings') {
        return prev.filter(({ action }) => !['unpublish', 'delete','duplicate'].includes(action))
      }
      return prev
    },
  },
});

// sanity.cli.js
import {defineCliConfig} from 'sanity/cli'

export default defineCliConfig({
  api: {
    projectId: 'XX', // replace value with your own
    dataset: 'production' // replace value with your own
  }
})

Hey
user P
! If you’re defining two different environments, you need to pass an array into your
defineConfig
. Just wrap your two config objects in an array and you should be all set! You’re probably getting the workspace not defined error because you’re trying to navigate to something like
<http://localhost:3333/desk>
instead of
<http://localhost:3333/<basePath>/desk>
.
Doh! Such a noobie mistake! Works like charm now, thank you!
Haha, I’ve made that exact mistake before.

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?