Previews
Per-Branch Preview URLs
Most teams deploying to platforms like Vercel use unique hostnames per preview build. Zero supports this directly, and you do not need one zero-cache instance per preview deployment.
Overview
Preview support has two parts:
- Configure
zero-cachewith allowed URL patterns for both query and mutate endpoints. - In the browser, pick the concrete
queryURLandmutateURLbased on the current hostname when constructingZero.
You must do this for both endpoints.
Configure Allowed Endpoint Patterns
Set ZERO_QUERY_URL and ZERO_MUTATE_URL to include your production URL and your preview URL pattern:
ZERO_QUERY_URL="https://myapp.com/api/zero/query,https://my-app-*.preview.myapp.com/api/zero/query"
ZERO_MUTATE_URL="https://myapp.com/api/zero/mutate,https://my-app-*.preview.myapp.com/api/zero/mutate"zero-cache will only allow client-selected URLs that match one of the configured values/patterns.
Choose Endpoint URLs in the Client
When you construct Zero on the client, derive URLs from location.origin and pass both queryURL and mutateURL:
function getZeroEndpoints() {
const origin = location.origin
return {
queryURL: `${origin}/api/zero/query`,
mutateURL: `${origin}/api/zero/mutate`
}
}
const {queryURL, mutateURL} = getZeroEndpoints()
const zero = new Zero({
schema,
userID,
auth,
queryURL,
mutateURL
})For full URL pattern syntax details, see Queries URL Patterns.
Schema Changes in Previews
If a preview includes a schema change, you should implement the schema change first and do it in the same backwards-compatible way documented in Schema Changes (expand → migrate → contract). After that, implement the rest of the preview behavior.
In practice, this means:
- Apply the compatible schema expansion first.
- Ship the preview app/API behavior as a preview, using the new schema.
- Run contract cleanup later, after old clients are gone.
If desired, previews can share a single staging database.
Neon-style per-preview database branching is not well supported with Zero today, because each upstream database typically needs its own zero-cache.