|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect } from "react"; |
| 4 | + |
| 5 | +import { isLocal } from "@/server/isLocal"; |
| 6 | + |
| 7 | +export function WebSocketRefresh() { |
| 8 | + useEffect(() => { |
| 9 | + if (!isLocal()) { |
| 10 | + console.log("Not in local mode, skipping WebSocket connection"); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + // Ensure we're in a browser environment |
| 15 | + if (typeof window === "undefined") { |
| 16 | + console.log("Not in browser environment, skipping WebSocket connection"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // Ensure WebSocket is available |
| 21 | + if (typeof WebSocket === "undefined") { |
| 22 | + console.error("WebSocket is not available in this environment"); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + // Get the current port from the window location |
| 27 | + const port = process.env.NEXT_PUBLIC_FDR_ORIGIN || 3001; |
| 28 | + const wsUrl = `ws://localhost:${port}`; |
| 29 | + |
| 30 | + console.log(`Attempting to connect to WebSocket server at ${wsUrl}...`); |
| 31 | + |
| 32 | + let ws: WebSocket | null = null; |
| 33 | + let timeout: NodeJS.Timeout | null = null; |
| 34 | + |
| 35 | + try { |
| 36 | + ws = new WebSocket(wsUrl); |
| 37 | + |
| 38 | + ws.onopen = () => { |
| 39 | + console.log("Client: Successfully connected to WebSocket server"); |
| 40 | + }; |
| 41 | + |
| 42 | + ws.onmessage = async (event) => { |
| 43 | + console.log("Client: Received WebSocket message:", event.data); |
| 44 | + |
| 45 | + try { |
| 46 | + const message = JSON.parse(event.data); |
| 47 | + console.log("Client: Parsed message:", message); |
| 48 | + |
| 49 | + if (message.type === "finishReload") { |
| 50 | + console.log( |
| 51 | + "Client: Received finishReload message, calling revalidate-local endpoint" |
| 52 | + ); |
| 53 | + try { |
| 54 | + const response = await fetch("/api/fern-docs/revalidate-local"); |
| 55 | + if (!response.ok) { |
| 56 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 57 | + } |
| 58 | + console.log("Client: Successfully revalidated"); |
| 59 | + // Reload the page after revalidation |
| 60 | + window.location.reload(); |
| 61 | + } catch (error) { |
| 62 | + console.error("Client: Failed to revalidate:", error); |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + console.error("Client: Failed to parse WebSocket message:", error); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + ws.onerror = (error) => { |
| 71 | + console.error("Client: WebSocket error:", error); |
| 72 | + }; |
| 73 | + |
| 74 | + ws.onclose = (event) => { |
| 75 | + console.log( |
| 76 | + `Client: WebSocket connection closed. Code: ${event.code}, Reason: ${event.reason}` |
| 77 | + ); |
| 78 | + }; |
| 79 | + |
| 80 | + // Add connection timeout check |
| 81 | + timeout = setTimeout(() => { |
| 82 | + if (ws?.readyState !== WebSocket.OPEN) { |
| 83 | + console.error( |
| 84 | + "Client: WebSocket connection failed to establish within 5 seconds" |
| 85 | + ); |
| 86 | + } |
| 87 | + }, 5000); |
| 88 | + } catch (error) { |
| 89 | + console.error("Client: Failed to create WebSocket connection:", error); |
| 90 | + } |
| 91 | + |
| 92 | + return () => { |
| 93 | + console.log("Client: Cleaning up WebSocket connection"); |
| 94 | + if (timeout) { |
| 95 | + clearTimeout(timeout); |
| 96 | + } |
| 97 | + if (ws) { |
| 98 | + ws.close(); |
| 99 | + } |
| 100 | + }; |
| 101 | + }, []); |
| 102 | + |
| 103 | + return null; |
| 104 | +} |
0 commit comments