fix: phần code build android
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Smart runtime backend URL resolver.
|
||||
*
|
||||
* Eliminates the need to manually update `.env` files when switching between:
|
||||
* - Docker/Linux Production Web Server → uses window.location.origin
|
||||
* - Android APK (Capacitor WebView) → forces absolute production domain
|
||||
*
|
||||
* Priority order:
|
||||
* 1. Explicit VITE_BACKEND_URL env variable (if set at build time)
|
||||
* 2. Android/Capacitor app detection → https://yotrip.labz.io.vn
|
||||
* 3. Production web server → window.location.origin
|
||||
*/
|
||||
|
||||
const PRODUCTION_DOMAIN = 'https://yotrip.labz.io.vn';
|
||||
|
||||
const resolveBackendEndpoint = (): string => {
|
||||
// 1. Honour explicit build-time env override first
|
||||
const envUrl = import.meta.env.VITE_BACKEND_URL as string | undefined;
|
||||
if (envUrl) {
|
||||
return envUrl;
|
||||
}
|
||||
|
||||
const origin = window.location.origin;
|
||||
const userAgent = navigator.userAgent.toLowerCase();
|
||||
|
||||
// 2. Detect Android APK / Capacitor WebView environment
|
||||
// These shells run on local/file:// origins, not the production domain.
|
||||
const isAndroidApp =
|
||||
origin.startsWith('http://localhost') ||
|
||||
origin.startsWith('capacitor://') ||
|
||||
origin.startsWith('file://') ||
|
||||
(userAgent.includes('android') && !origin.includes('yotrip.labz.io.vn'));
|
||||
|
||||
if (isAndroidApp) {
|
||||
console.log('[Backend] 📱 Android/Capacitor environment detected — using production domain.');
|
||||
return PRODUCTION_DOMAIN;
|
||||
}
|
||||
|
||||
// 3. Web production server: the origin IS the backend (same Docker host)
|
||||
return origin;
|
||||
};
|
||||
|
||||
/** Resolved backend base URL for the current runtime environment. */
|
||||
export const BACKEND_URL: string = resolveBackendEndpoint();
|
||||
|
||||
console.log(`[Backend] 🌐 Active endpoint: ${BACKEND_URL}`);
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
|
||||
import { LocalNotifications } from '@capacitor/local-notifications';
|
||||
import { BACKEND_URL } from './backendEndpoint';
|
||||
|
||||
function rewriteUrls(obj: any, backendUrl: string): any {
|
||||
if (obj === null || obj === undefined) return obj;
|
||||
@@ -47,7 +48,7 @@ if (Capacitor.isNativePlatform()) {
|
||||
console.error('[LocalNotifications] Failed to request permissions:', e);
|
||||
}
|
||||
|
||||
const backendUrl = import.meta.env.VITE_BACKEND_URL || 'https://yotrip.labz.io.vn';
|
||||
const backendUrl = BACKEND_URL;
|
||||
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
|
||||
Reference in New Issue
Block a user