diff --git a/src/lib/api-fetch.ts b/src/lib/api-fetch.ts index 8d935716..2f9c7546 100644 --- a/src/lib/api-fetch.ts +++ b/src/lib/api-fetch.ts @@ -1,10 +1,25 @@ import { AppServer } from '@/app/api/[[...slugs]]/route' import { treaty } from '@elysiajs/eden' -// Use relative URL '/' for better deployment flexibility -// This allows the API to work correctly in both development and staging/production -const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || '/' +// Determine the base URL based on environment +// treaty requires a full URL, cannot use relative paths like '/' +const getBaseUrl = () => { + // Development (server-side) + if (process.env.NODE_ENV === 'development' && typeof window === 'undefined') { + return process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000' + } + + // Client-side (browser) - use current window origin + if (typeof window !== 'undefined') { + return window.location.origin + } + + // Production/Staging server-side - use environment variable or default + return process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000' +} + +const BASE_URL = getBaseUrl() const ApiFetch = treaty(BASE_URL) -export default ApiFetch \ No newline at end of file +export default ApiFetch