Enhance socket.io URL handling for production environments

- Added logic to normalize the socket.io URL based on the current origin and environment.
- Implemented fallback mechanisms for unusual ports in production to ensure secure connections.
- Included error handling for URL parsing failures to default to the current origin, improving robustness.
This commit is contained in:
Torsten Schulz (local)
2026-01-05 16:26:23 +01:00
parent 8e618ab443
commit 0336c55560

View File

@@ -187,6 +187,30 @@ const store = createStore({
if (!socketIoUrl && (import.meta.env.DEV || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')) {
socketIoUrl = 'http://localhost:3001';
}
// Normalisiere URL (Env-Variablen enthalten teils Ports/Pfade wie :4443 oder /api)
// In Produktion sollte Socket.IO idealerweise über den gleichen Origin (443) laufen (Reverse-Proxy),
// damit kein separater Port im Browser benötigt wird.
try {
const hostname = window.location.hostname;
const isProduction = hostname === 'www.your-part.de' || hostname.includes('your-part.de');
if (socketIoUrl) {
const parsed = new URL(socketIoUrl, window.location.origin);
// Falls /api oder ähnliche Pfade enthalten sind → auf Origin reduzieren
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) {
// Wenn Parsing fehlschlägt: letzte Rettung ist der aktuelle Origin
try {
socketIoUrl = window.location.origin;
} catch (_) {}
}
const socket = io(socketIoUrl, {
secure: true,