This Friday: Hear from Sanity + Vercel experts on AI and better personalization in e-commerce

Sanity dev server not loading in GitHub Codespaces - port forwarding issue

1 replyLast updated: Nov 29, 2025

Hello everyone,
I'm experiencing some difficulties using Github's Codespaces and Sanity. While I'm able to initialize and code a Sanity project within a Codespace, I'm having trouble starting the dev server with "npm run dev" as the page keeps loading indefinitely. I'm assuming this is related to the port forwarding logic that Codespaces use. Have any of you encountered a similar issue?

Thank you.
AI Update

You're absolutely right that this is related to how GitHub Codespaces handles port forwarding! This is a common issue when running Sanity Studio (which uses Vite) in Codespaces.

The problem occurs because the dev server binds to localhost by default, which doesn't work well with Codespaces' port forwarding mechanism. Here are the solutions:

Quick Fix: Bind to 0.0.0.0

Modify your dev command to bind to all network interfaces:

sanity dev --host 0.0.0.0

Or update your package.json:

{
  "scripts": {
    "dev": "sanity dev --host 0.0.0.0"
  }
}

Advanced Fix: Configure Vite for Codespaces

For a more robust solution, create or update your sanity.cli.ts file to configure Vite's server and HMR (Hot Module Replacement) settings:

import {defineCliConfig} from 'sanity/cli'
import {type UserConfig} from 'vite'

export default defineCliConfig({
  vite(viteConfig: UserConfig): UserConfig {
    return {
      ...viteConfig,
      server: {
        ...viteConfig.server,
        host: '0.0.0.0',
        hmr: {
          clientPort: 443,
          host: process.env.CODESPACE_NAME 
            ? `${process.env.CODESPACE_NAME}-3333.app.github.dev` 
            : 'localhost'
        }
      }
    }
  }
})

This configuration:

Why This Happens

Sanity Studio uses Vite as its build tooling, and Vite's dev server needs to be accessible through Codespaces' port forwarding system. By default, it only listens on localhost, which isn't accessible through the forwarded ports. The --host 0.0.0.0 flag tells Vite to listen on all network interfaces, making it accessible through Codespaces' proxy.

The HMR configuration is important because without it, hot module replacement might try to connect to the wrong URL, causing the infinite loading you're experiencing.

Additional Tips

The --host 0.0.0.0 flag is usually sufficient for most cases, but the full Vite configuration ensures HMR works properly too!

Show original thread
1 reply

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