From c9141e3b35aca9c15eb6dd25a8e50a51bdc94ee3 Mon Sep 17 00:00:00 2001 From: Till JS Date: Tue, 21 Apr 2026 13:55:32 +0200 Subject: [PATCH] fix(spaces): keep process.env out of the browser branch in authBaseUrl Opening the Space-Switcher threw "process is not defined" because the missing-injection fallback inside the browser branch returned early only when the injected URL was truthy, then fell through to a `process.env.PUBLIC_MANA_AUTH_URL` read. `process` doesn't exist in the browser, so empty injection = ReferenceError. Fix: the browser branch now always returns (injected or localhost:3001 fallback) without touching `process`. The env-var read stays inside the SSR path and is further guarded by `typeof process !== 'undefined'` so a future change can't regress. 0 errors across 7203 files. Plan: docs/plans/spaces-foundation.md Co-Authored-By: Claude Opus 4.7 (1M context) --- apps/mana/apps/web/src/lib/data/scope/auth-fetch.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/mana/apps/web/src/lib/data/scope/auth-fetch.ts b/apps/mana/apps/web/src/lib/data/scope/auth-fetch.ts index e344c13b1..58f7e5bcc 100644 --- a/apps/mana/apps/web/src/lib/data/scope/auth-fetch.ts +++ b/apps/mana/apps/web/src/lib/data/scope/auth-fetch.ts @@ -15,12 +15,19 @@ import { browser } from '$app/environment'; export function authBaseUrl(): string { + // Browser: read the runtime-injected URL, fall back to localhost dev. + // `process` is not defined in browsers, so the env-var branch below + // MUST stay inside the server-side path to avoid a ReferenceError. if (browser && typeof window !== 'undefined') { const injected = (window as unknown as { __PUBLIC_MANA_AUTH_URL__?: string }) .__PUBLIC_MANA_AUTH_URL__; - if (injected) return injected.replace(/\/$/, ''); + return (injected || 'http://localhost:3001').replace(/\/$/, ''); } - return (process.env.PUBLIC_MANA_AUTH_URL || 'http://localhost:3001').replace(/\/$/, ''); + // SSR: Node has `process.env`. + const fromEnv = + (typeof process !== 'undefined' && process.env?.PUBLIC_MANA_AUTH_URL) || + 'http://localhost:3001'; + return fromEnv.replace(/\/$/, ''); } /**