Cross-Origin Resource Sharing
Cross-Origin Resource Sharing (CORS) is a security mechanism that allows a web application running at one origin to access resources from a different origin. Without CORS, browsers block cross-origin HTTP requests by default as a security measure.
You'll need to configure CORS when:
- Local Development: You're developing locally and your client runs on a different port than your actor service
- Different Domain: Your frontend application is hosted on a different domain than your actor service
Registry-Level CORS
Configure CORS directly in your registry setup for applications that do not require configuring CORS on their own endpoints.
Configuration Options
origin
string | string[] | (origin: string) => boolean | string
Specifies which domains can access your resources:
allowMethods
string[]
HTTP methods clients are allowed to use:
allowHeaders
string[]
Headers that clients can send in requests:
credentials
boolean
Whether to allow credentials (cookies, auth headers):
When credentials: true
, you cannot use origin: "*"
. Specify exact origins instead.
maxAge
number
How long browsers cache CORS preflight responses (in seconds):
exposeHeaders
string[]
Server headers that browsers can access:
Router-Level CORS
For applications that need to expose their own routes, configure CORS at the router level:
Required Headers for Rivet
Rivet requires specific headers for communication. Always include ALLOWED_PUBLIC_HEADERS
:
These are automatically configured if using registry.runServer({ cors })
.
Without ALLOWED_PUBLIC_HEADERS
, Rivet clients won't be able to communicate with your actors from the browser.
Development vs Production
Development Setup
For local development, allow localhost origins:
Production Setup
For production, be restrictive with origins:
Troubleshooting
Common CORS Errors
"Access to fetch blocked by CORS policy"
- Add your frontend's origin to the
origin
list - Ensure
ALLOWED_PUBLIC_HEADERS
are included inallowHeaders
"Request header not allowed"
- Add the missing header to
allowHeaders
- Include
ALLOWED_PUBLIC_HEADERS
in your configuration
"Credentials mode mismatch"
- Set
credentials: true
in CORS config - Cannot use
origin: "*"
with credentials