hot fix #2 dev day #70

This commit is contained in:
Tomas Dvorak
2025-10-24 16:28:31 +02:00
parent d7eb938d3a
commit 80f833b926
21 changed files with 115 additions and 17 deletions
+4 -2
View File
@@ -4,15 +4,17 @@ import { getToken } from '../utils/auth';
// Resolve API URL. Some code uses REACT_APP_API_URL (full api path including /api/v1),
// others set REACT_APP_API_BASE_URL (backend origin). Normalize so baseURL always points to API root.
const envApiUrl = process.env.REACT_APP_API_URL || process.env.REACT_APP_API_BASE_URL;
let API_URL = envApiUrl || 'http://localhost:8080/api/v1';
let API_URL = envApiUrl || '/api/v1';
// If the provided base looks like a backend origin (no /api/), append /api/v1
try {
const maybe = new URL(API_URL);
const maybe = new URL(API_URL, typeof window !== 'undefined' ? window.location.origin : undefined);
if (!/\/api\//.test(maybe.pathname)) {
// ensure single trailing slash then append api/v1
maybe.pathname = maybe.pathname.replace(/\/$/, '') + '/api/v1';
API_URL = maybe.toString();
} else {
API_URL = maybe.toString();
}
} catch {
// If URL parsing fails, keep API_URL as-is
+18 -6
View File
@@ -8,13 +8,25 @@ export function assetUrl(pathOrUrl?: string | null): string | undefined {
if (/^(?:https?:)?\/\//i.test(pathOrUrl) || /^data:/i.test(pathOrUrl)) {
return pathOrUrl;
}
// Rewrite known backend-served asset paths (/uploads, /dist)
// Known backend-served asset paths (/uploads, optionally /dist)
if (pathOrUrl.startsWith('/uploads') || pathOrUrl.startsWith('/dist')) {
const base = process.env.REACT_APP_API_BASE_URL || process.env.REACT_APP_API_URL || 'http://localhost:8080/api/v1';
// We want the backend origin, not the API path. Construct from the base.
const baseUrl = new URL(base);
baseUrl.pathname = '/';
return new URL(pathOrUrl, baseUrl).toString();
// 1) Explicit override wins
const explicit = process.env.REACT_APP_ASSET_BASE_URL || process.env.REACT_APP_API_BASE_URL || process.env.REACT_APP_API_URL || '';
if (explicit) {
const baseUrl = new URL(explicit, typeof window !== 'undefined' ? window.location.origin : undefined);
baseUrl.pathname = '/';
return new URL(pathOrUrl, baseUrl).toString();
}
// 2) Local dev/IP: talk to backend:8080 directly
if (typeof window !== 'undefined') {
const { protocol, hostname } = window.location;
const isLocal = hostname === 'localhost' || /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
if (isLocal) {
return `${protocol}//${hostname}:8080${pathOrUrl}`;
}
}
// 3) Production/domain: keep relative so frontend Nginx/Cloudflared path proxy forwards to backend
return pathOrUrl;
}
// Otherwise return as-is (relative or other paths)
return pathOrUrl;