@@ -7,33 +7,38 @@ import { isLocal } from "@/server/isLocal";
7
7
export function WebSocketRefresh ( ) {
8
8
useEffect ( ( ) => {
9
9
let ws : WebSocket | null = null ;
10
- let timeout : NodeJS . Timeout | null = null ;
10
+ let connectionTimeout : NodeJS . Timeout | null = null ;
11
11
12
12
const setupWebSocket = async ( ) : Promise < void > => {
13
13
if ( ! isLocal ( ) ) {
14
14
console . log ( "Not in local mode, skipping WebSocket connection" ) ;
15
15
return ;
16
16
}
17
17
18
- // Ensure we're in a browser environment
19
18
if ( typeof window === "undefined" ) {
20
19
console . log (
21
20
"Not in browser environment, skipping WebSocket connection"
22
21
) ;
23
22
return ;
24
23
}
25
24
26
- // Ensure WebSocket is available
27
25
if ( typeof WebSocket === "undefined" ) {
28
26
console . error ( "WebSocket is not available in this environment" ) ;
29
27
return ;
30
28
}
31
29
32
- const response = await fetch ( "/api/fern-docs/env-local" ) ;
33
- if ( ! response . ok ) {
34
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
30
+ // revalidate the page first to clear any cached content
31
+ const revalidateResponse = await fetch ( "/api/fern-docs/revalidate-local" ) ;
32
+ if ( ! revalidateResponse . ok ) {
33
+ throw new Error ( `HTTP error! status: ${ revalidateResponse . status } ` ) ;
35
34
}
36
- const data = await response . json ( ) ;
35
+ console . log ( "Client: Successfully revalidated" ) ;
36
+
37
+ const envResponse = await fetch ( "/api/fern-docs/env-local" ) ;
38
+ if ( ! envResponse . ok ) {
39
+ throw new Error ( `HTTP error! status: ${ envResponse . status } ` ) ;
40
+ }
41
+ const data = await envResponse . json ( ) ;
37
42
38
43
if ( ! data . backendPort ) {
39
44
console . error ( "No port found in env-local response" ) ;
@@ -58,6 +63,8 @@ export function WebSocketRefresh() {
58
63
const message = JSON . parse ( event . data ) ;
59
64
console . log ( "Client: Parsed message:" , message ) ;
60
65
66
+ // revalidate the docs when a change has been detected
67
+ // todo: only revalidate the relevant part of the docs (e.g. the page that changed)
61
68
if ( message . type === "finishReload" ) {
62
69
console . log (
63
70
"Client: Received finishReload message, calling revalidate-local endpoint"
@@ -68,12 +75,16 @@ export function WebSocketRefresh() {
68
75
throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
69
76
}
70
77
console . log ( "Client: Successfully revalidated" ) ;
71
- // Reload the page after revalidation
72
78
window . location . reload ( ) ;
73
79
} catch ( error ) {
74
80
console . error ( "Client: Failed to revalidate:" , error ) ;
75
81
}
76
82
}
83
+
84
+ if ( message . type === "ping" && ws ?. readyState === WebSocket . OPEN ) {
85
+ console . log ( "Client: Received ping, sending pong" ) ;
86
+ ws . send ( JSON . stringify ( { type : "pong" } ) ) ;
87
+ }
77
88
} catch ( error ) {
78
89
console . error ( "Client: Failed to parse WebSocket message:" , error ) ;
79
90
}
@@ -89,8 +100,7 @@ export function WebSocketRefresh() {
89
100
) ;
90
101
} ;
91
102
92
- // Add connection timeout check
93
- timeout = setTimeout ( ( ) => {
103
+ connectionTimeout = setTimeout ( ( ) => {
94
104
if ( ws ?. readyState !== WebSocket . OPEN ) {
95
105
console . error (
96
106
"Client: WebSocket connection failed to establish within 5 seconds"
@@ -106,8 +116,8 @@ export function WebSocketRefresh() {
106
116
107
117
return ( ) => {
108
118
console . log ( "Client: Cleaning up WebSocket connection" ) ;
109
- if ( timeout ) {
110
- clearTimeout ( timeout ) ;
119
+ if ( connectionTimeout ) {
120
+ clearTimeout ( connectionTimeout ) ;
111
121
}
112
122
if ( ws ) {
113
123
ws . close ( ) ;
0 commit comments