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 simple cases:
This approach works well for basic setups but has limitations for complex scenarios.
Router-Level CORS (Recommended)
For production applications, configure CORS at the router level for maximum control:
Required Headers for Rivet
Rivet requires specific headers for communication. Always include ALLOWED_PUBLIC_HEADERS
:
Notice
Without ALLOWED_PUBLIC_HEADERS
, Rivet clients won't be able to communicate with your actors from the browser.
Framework-Specific Examples
Express.js
Elysia
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):
Notice
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:
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
Debug CORS Issues
Enable CORS logging to debug issues:
Best Practices
- Use Router-Level CORS: More flexible than registry-level configuration
- Include ALLOWED_PUBLIC_HEADERS: Required for Rivet communication
- Specify Exact Origins: Avoid wildcards in production
- Enable Credentials: Needed for authentication
- Cache Preflight Requests: Use appropriate
maxAge
values - Environment-Specific Config: Different settings for dev/prod