Przeglądaj źródła

Fix multi-table WebSocket connections failing in production

The issue was that table switching worked in development but failed in
production (port 80). Two problems were fixed:

1. Removed reliance on stale `isCurrent` flag from localStorage which
   could incorrectly prevent the base URL from being set
2. Added URL normalization for comparison to handle port differences
   (e.g., http://host:80 vs http://host are now correctly recognized
   as the same origin)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 2 tygodni temu
rodzic
commit
f0d6371d85
1 zmienionych plików z 19 dodań i 3 usunięć
  1. 19 3
      frontend/src/contexts/TableContext.tsx

+ 19 - 3
frontend/src/contexts/TableContext.tsx

@@ -43,6 +43,19 @@ const TableContext = createContext<TableContextType | null>(null)
 const STORAGE_KEY = 'duneweaver_tables'
 const ACTIVE_TABLE_KEY = 'duneweaver_active_table'
 
+/**
+ * Normalize a URL to its origin for comparison purposes.
+ * This handles port normalization (e.g., :80 for HTTP is stripped).
+ * Returns the origin or the original string if parsing fails.
+ */
+function normalizeUrlOrigin(url: string): string {
+  try {
+    return new URL(url).origin
+  } catch {
+    return url
+  }
+}
+
 interface StoredTableData {
   tables: Table[]
   activeTableId: string | null
@@ -75,8 +88,10 @@ export function TableProvider({ children }: { children: React.ReactNode }) {
           if (active) {
             restoredActiveIdRef.current = activeId // Mark that we restored a selection
             setActiveTableState(active)
-            // Only set non-empty base URL for remote tables
-            if (!active.isCurrent && active.url !== window.location.origin) {
+            // Set base URL for remote tables (tables not on the current origin)
+            // Use normalized URL comparison to handle port differences (e.g., :80 vs no port)
+            // Don't rely on isCurrent flag as it may be stale from localStorage
+            if (normalizeUrlOrigin(active.url) !== window.location.origin) {
               apiClient.setBaseUrl(active.url)
             }
           }
@@ -127,7 +142,8 @@ export function TableProvider({ children }: { children: React.ReactNode }) {
     }
 
     // Update API client base URL
-    if (table.isCurrent || table.url === window.location.origin) {
+    // Use normalized URL comparison to handle port differences (e.g., :80 vs no port)
+    if (normalizeUrlOrigin(table.url) === window.location.origin) {
       apiClient.setBaseUrl('')
     } else {
       apiClient.setBaseUrl(table.url)