From e3135ccde61c8bed2398962c1a24548a1f774757 Mon Sep 17 00:00:00 2001 From: Genet Schneider Date: Tue, 9 Nov 2021 23:26:55 +0200 Subject: [PATCH] Support assetPrefix specific protocol (#31213) Co-authored-by: JJ Kasper --- packages/next/client/dev/error-overlay/websocket.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/next/client/dev/error-overlay/websocket.ts b/packages/next/client/dev/error-overlay/websocket.ts index 58dcee1a49c3212..46f81de4c4a7b10 100644 --- a/packages/next/client/dev/error-overlay/websocket.ts +++ b/packages/next/client/dev/error-overlay/websocket.ts @@ -2,6 +2,17 @@ let source: WebSocket const eventCallbacks: ((event: any) => void)[] = [] let lastActivity = Date.now() +function getSocketProtocol(assetPrefix: string): string { + let protocol = location.protocol + + try { + // assetPrefix is a url + protocol = new URL(assetPrefix).protocol + } catch (_) {} + + return protocol === 'http:' ? 'ws' : 'wss' +} + export function addMessageListener(cb: (event: any) => void) { eventCallbacks.push(cb) } @@ -32,7 +43,7 @@ export function connectHMR(options: { function init() { if (source) source.close() const { hostname, port } = location - const protocol = location.protocol === 'http:' ? 'ws' : 'wss' + const protocol = getSocketProtocol(options.assetPrefix || '') const assetPrefix = options.assetPrefix.replace(/^\/+/, '') let url = `${protocol}://${hostname}:${port}${