🚀 Deployment Fix: Add TypeScript env types and SSR-safe URL detection

- Created vite-env.d.ts with proper ImportMetaEnv interface
- Fixed client.ts to use internal Docker URL (http://directus:8055) for SSR
- Removed @ts-ignore directives
- Frontend now compiles cleanly
This commit is contained in:
cawcenter
2025-12-14 13:28:25 -05:00
parent a74a4e946d
commit 25c934489c
2 changed files with 58 additions and 10 deletions

View File

@@ -13,20 +13,29 @@ import {
import type { DirectusSchema } from '../schemas';
import type { DirectusClient, RestClient } from '@directus/sdk';
// @ts-ignore
/**
* SSR-Safe Directus URL Detection
*
* When running Server-Side (SSR), the frontend container cannot reach
* the public HTTPS URL from inside Docker. It must use the internal
* Docker network name 'directus' instead.
*
* - SSR/Server: http://directus:8055 (internal Docker network)
* - Browser/Client: https://spark.jumpstartscaling.com (public URL)
*/
const isServer = import.meta.env.SSR || typeof window === 'undefined';
// Public URL for client-side requests
const PUBLIC_URL = import.meta.env.PUBLIC_DIRECTUS_URL || 'https://spark.jumpstartscaling.com';
// Internal URL (SSR only) - used when running server-side requests
const INTERNAL_URL = typeof process !== 'undefined' && process.env?.INTERNAL_DIRECTUS_URL
? process.env.INTERNAL_DIRECTUS_URL
: 'https://spark.jumpstartscaling.com';
// Internal Docker URL for SSR requests
const INTERNAL_URL = 'http://directus:8055';
// @ts-ignore
const DIRECTUS_TOKEN = import.meta.env.DIRECTUS_ADMIN_TOKEN || (typeof process !== 'undefined' && process.env ? process.env.DIRECTUS_ADMIN_TOKEN : '') || 'eufOJ_oKEx_FVyGoz1GxWu6nkSOcgIVS';
// Select URL based on environment
const DIRECTUS_URL = isServer ? INTERNAL_URL : PUBLIC_URL;
// Select URL based on environment (Server vs Client)
// Always use the public URL to ensure consistent access
const DIRECTUS_URL = PUBLIC_URL;
// Admin token for authenticated requests
const DIRECTUS_TOKEN = import.meta.env.DIRECTUS_ADMIN_TOKEN || '';
/**
* Creates a typed Directus client for the Spark Platform

39
frontend/src/vite-env.d.ts vendored Normal file
View File

@@ -0,0 +1,39 @@
/// <reference types="vite/client" />
/// <reference types="astro/client" />
/**
* Spark Platform Environment Variables
* These are injected at build/runtime via Astro's import.meta.env
*/
interface ImportMetaEnv {
/** Public Directus API URL (e.g., https://spark.jumpstartscaling.com) */
readonly PUBLIC_DIRECTUS_URL: string;
/** Admin token for authenticated API requests (optional, for SSR) */
readonly DIRECTUS_ADMIN_TOKEN?: string;
/** Public platform domain for generating URLs */
readonly PUBLIC_PLATFORM_DOMAIN?: string;
/** Preview domain for draft content */
readonly PREVIEW_DOMAIN?: string;
/** True when running on server (SSR mode) */
readonly SSR: boolean;
/** True during development */
readonly DEV: boolean;
/** True for production build */
readonly PROD: boolean;
/** Base URL of the site */
readonly BASE_URL: string;
/** Current mode (development, production, etc.) */
readonly MODE: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}