Files
starter-e2e-mrf9dd3o/apps/app/src/lib/env.ts
2026-07-10 20:17:46 +02:00

19 lines
1010 B
TypeScript

// Endpoints come from env, never hardcoded.
//
// In local dev VITE_API_URL is set at build time via .dev.vars and Vite
// replaces the import.meta.env reference inline. In CF Workers deployments
// VITE_API_URL is a runtime text binding — invisible to Vite at build time —
// so import.meta.env.VITE_API_URL is undefined in the bundle. We fall back to
// the current origin + /api, which is correct because the dispatcher worker
// routes every /api/* path to the API units on the same hostname.
export function apiUrl(): string {
if (import.meta.env.SSR) {
// SSR context: PikkuProvider is a client-side provider and its serverUrl
// is only consumed by hooks that run in the browser. Return the build-time
// var when available (local dev) or a placeholder so SSR never throws.
return import.meta.env.VITE_API_URL ?? '/__api'
}
// Client: use the build-time var (local dev) or derive from current origin.
return import.meta.env.VITE_API_URL ?? window.location.origin + '/api'
}