Codespaces Doesn't Allow Requests Across Ports From a Preview

At time of writing, a CORS error will occur if the web application served in a GitHub preview URL attempts to make a fetch request to a different port. Specifically, it the preflight OPTION request receive a 302 HTTP status code and fail with Redirect is not allowed for a preflight request.

Forwarding ports in a Codespace will generate a different URL for each port. For example, opening port 8000 will have a preview URL of {codespace name}-{port} That means, if you have your frontend and backend servers on different ports, it won’t work.

The workaround is to change the port from private (the default) to public. Annoyingly, there is no way to configure that a port be made public in the devcontainer.json so it needs to be set each time a codespace is created. The other way around is to add a reverse proxy to put both frontend and backend server on the same port, but that may not match what is in your production setup.

See also: