Refactor socket.io URL normalization logic for improved clarity and robustness

- Simplified the normalization process for socket.io URLs by removing unnecessary production checks.
- Enhanced comments for better understanding of URL handling, particularly regarding environment variables and port management.
- Maintained fallback mechanisms for URL parsing failures to ensure consistent behavior across environments.
This commit is contained in:
Torsten Schulz (local)
2026-01-05 16:43:42 +01:00
parent 0336c55560
commit dab3391aa2

View File

@@ -188,22 +188,12 @@ const store = createStore({
socketIoUrl = 'http://localhost:3001'; socketIoUrl = 'http://localhost:3001';
} }
// Normalisiere URL (Env-Variablen enthalten teils Ports/Pfade wie :4443 oder /api) // Normalisiere URL (Env-Variablen enthalten teils Pfade wie /api; Port kann absichtlich gesetzt sein, z.B. :4443)
// In Produktion sollte Socket.IO idealerweise über den gleichen Origin (443) laufen (Reverse-Proxy),
// damit kein separater Port im Browser benötigt wird.
try { try {
const hostname = window.location.hostname;
const isProduction = hostname === 'www.your-part.de' || hostname.includes('your-part.de');
if (socketIoUrl) { if (socketIoUrl) {
const parsed = new URL(socketIoUrl, window.location.origin); const parsed = new URL(socketIoUrl, window.location.origin);
// Falls /api oder ähnliche Pfade enthalten sind → auf Origin reduzieren // Falls /api oder ähnliche Pfade enthalten sind → auf Origin reduzieren (inkl. Port!)
socketIoUrl = parsed.origin; socketIoUrl = parsed.origin;
// Falls in Produktion ein unüblicher Port gesetzt ist (z.B. :4443) → auf aktuellen Origin zurückfallen
if (isProduction && parsed.hostname === hostname && parsed.port && parsed.port !== '443') {
console.warn('[socket.io] Ungewöhnlicher Port in VITE_SOCKET_IO_URL erkannt, fallback auf window.location.origin:', parsed.origin, '→', window.location.origin);
socketIoUrl = window.location.origin;
}
} }
} catch (e) { } catch (e) {
// Wenn Parsing fehlschlägt: letzte Rettung ist der aktuelle Origin // Wenn Parsing fehlschlägt: letzte Rettung ist der aktuelle Origin