chore: heygermany customer project
This commit is contained in:
23
apps/website/lib/env.ts
Normal file
23
apps/website/lib/env.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// Endpoints come from env, never hardcoded.
|
||||
//
|
||||
// In local dev VITE_API_URL is set at build time and Vite replaces the
|
||||
// import.meta.env reference inline. In deployed/sandbox bundles VITE_API_URL is
|
||||
// a runtime Worker binding — invisible to Vite at build time — so it's undefined
|
||||
// in the bundle. We fall back to the current origin + /api, which is correct
|
||||
// because the fabric dispatcher routes every /api/* path to the API on the same
|
||||
// host. (VITE_API_BASE_URL is accepted as a legacy alias for local dev.)
|
||||
export function apiUrl(): string {
|
||||
if (typeof window !== 'undefined' && (window as Window & { __E2E_API_URL?: string }).__E2E_API_URL) {
|
||||
return (window as Window & { __E2E_API_URL?: string }).__E2E_API_URL as string
|
||||
}
|
||||
const buildTime = import.meta.env.VITE_API_URL ?? import.meta.env.VITE_API_BASE_URL
|
||||
if (import.meta.env.SSR) {
|
||||
// SSR context: the auth client is only consumed in the browser. Return the
|
||||
// build-time var when available or a placeholder so SSR never throws.
|
||||
return buildTime ?? '/__api'
|
||||
}
|
||||
// Client: build-time var (local dev) or derive from the current origin. Better
|
||||
// Auth's client requires an absolute base URL, so never return a bare relative
|
||||
// '/api' here — '/api/auth' throws "Invalid base URL".
|
||||
return buildTime ?? `${window.location.origin}/api`
|
||||
}
|
||||
Reference in New Issue
Block a user