From 311b243b485d707a622fe728e0082b72bd034b6f Mon Sep 17 00:00:00 2001 From: Nathan Gaberel <nathan@gnab.fr> Date: Wed, 27 Mar 2019 22:08:08 +0000 Subject: [PATCH] Dev client: use current script URL when connecting to WS server. --- .../react-dev-utils/webpackHotDevClient.js | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/react-dev-utils/webpackHotDevClient.js b/packages/react-dev-utils/webpackHotDevClient.js index 050ce2a63..56a301da8 100644 --- a/packages/react-dev-utils/webpackHotDevClient.js +++ b/packages/react-dev-utils/webpackHotDevClient.js @@ -57,12 +57,37 @@ if (module.hot && typeof module.hot.dispose === 'function') { }); } +function getCurrentScriptSource() { + // `document.currentScript` is the most accurate way to find the current script, + // but is not supported in all browsers. + if (document.currentScript) { + return document.currentScript.getAttribute('src'); + } + + // Fall back to getting all scripts in the document. + const scriptElements = document.scripts || []; + const currentScript = scriptElements[scriptElements.length - 1]; + if (currentScript) { + return currentScript.getAttribute('src'); + } + + // Fall back to window.location. + return window.location.href; +} + +function getCurrentScriptURL() { + return new URL(getCurrentScriptSource(), window.location.href); +} + +var scriptUrl = getCurrentScriptURL(); + // Connect to WebpackDevServer via a socket. var connection = new SockJS( url.format({ - protocol: window.location.protocol, - hostname: window.location.hostname, - port: window.location.port, + protocol: scriptUrl.protocol, + hostname: scriptUrl.hostname, + port: scriptUrl.port, + auth: scriptUrl.auth, // Hardcoded in WebpackDevServer pathname: '/sockjs-node', }) -- GitLab