File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -57,10 +57,25 @@ export function getMainWebSocket(): WebSocketWrapper | null {
57
57
return mainWebSocket ;
58
58
}
59
59
60
+ // Modern browsers allow the WebSocket constructor to receive an http: or https: URL and implicitly convert it to a ws: or wss: URL.
61
+ // But, older browsers didn't support this, so we normalize the URL manually.
62
+ let normalizeWebSocketURL = ( url : string ) => {
63
+ const origin = globalThis ?. location ?. origin ?? globalThis ?. location ?. href ?? "http://localhost:3000" ;
64
+ let object = new URL ( url , origin ) ;
65
+ if ( object . protocol === "https:" ) {
66
+ object . protocol = "wss:" ;
67
+ } else if ( object . protocol === "http:" ) {
68
+ object . protocol = "ws:" ;
69
+ }
70
+
71
+ return object . toString ( ) ;
72
+ } ;
73
+
60
74
export function initWebSocket (
61
75
handlers : Record < number , ( dv : DataView < ArrayBuffer > , ws : WebSocket ) => void > ,
62
76
{ url = "/_bun/hmr" , onStatusChange } : { url ?: string ; onStatusChange ?: ( connected : boolean ) => void } = { } ,
63
77
) : WebSocketWrapper {
78
+ url = normalizeWebSocketURL ( url ) ;
64
79
let firstConnection = true ;
65
80
let closed = false ;
66
81
You can’t perform that action at this time.
0 commit comments