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

1 replies
Last updated: Mar 2, 2023
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:

  • Binds the server to 0.0.0.0 so it accepts connections from Codespaces' proxy
  • Sets the HMR client port to 443 (HTTPS)
  • Uses Codespaces environment variables to construct the correct forwarded URL (format: {codespace-name}-{port}.app.github.dev)

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

  • Make sure Codespaces has port 3333 set to "Public" visibility in the Ports panel if you're still having issues
  • You can also configure this in a .devcontainer/devcontainer.json file with "forwardPorts": [3333]
  • After making these changes, you should see the forwarded URL in Codespaces' Ports panel that you can click to access your Studio

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
Okay I fixed it and posting the solution in case anyone has the same issue:
By default, GitHub Codespaces forwards ports 3000, 4000, and 5000 to the host machine, which means that if your Sanity project is running on a different port, you may need to configure port forwarding to access it from your local machine.
To configure port forwarding in Codespaces, you can add a
.devcontainer/devcontainer.json
file to your project with the following configuration:
{
  "name": "Sanity Project",
  "appPort": 3333,
  "appPortAutoForward": true
}
Then, you'll probably get a CORS issue: "To access your content, you need to add the following URL as a CORS origin to your Sanity project".

To resolve this issue, you need to add the domain that you're trying to access as a CORS origin in your Sanity project. Here are the steps to do that:
1. Log in to your Sanity project dashboard.
2. Click on the "API Settings" button in the left-hand menu.
3. In the "CORS Origins" section, click on the "Add new origin" button.
4. Enter the domain that you're trying to access in the "Origin" field. For example`
https://127.0.0.1:3333 `5. Click on the "Save" button to save your changes.
Once you've added the domain as a CORS origin, you should be able to access the data from your Sanity project without any issues.

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?