Merge pull request #5 from bipprojectbali/fix-error-music-stg
Fix: Use window.location.origin for API base URL in browser
This commit is contained in:
@@ -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<AppServer>(BASE_URL)
|
||||
|
||||
export default ApiFetch
|
||||
export default ApiFetch
|
||||
|
||||
Reference in New Issue
Block a user