From e142fbd8be84f9dce6fdc28601e48b19dc23c27f Mon Sep 17 00:00:00 2001 From: Sean Mc Allister Date: Wed, 28 Sep 2016 17:55:00 +0200 Subject: [PATCH] resolve socksjs websocket host/port at runtime instead of assuming window.location --- .../react-dev-utils/webpackHotDevClient.js | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/react-dev-utils/webpackHotDevClient.js b/packages/react-dev-utils/webpackHotDevClient.js index ad8560fa043..395b75fae21 100644 --- a/packages/react-dev-utils/webpackHotDevClient.js +++ b/packages/react-dev-utils/webpackHotDevClient.js @@ -123,11 +123,31 @@ function showErrorOverlay(message) { }); } +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. + var scriptElements = document.scripts || []; + var currentScript = scriptElements[scriptElements.length - 1]; + if(currentScript) + return currentScript.getAttribute("src"); +} + + +var scriptHost = getCurrentScriptSource(); +scriptHost = scriptHost.replace(/\/[^\/]+$/, ""); + +var urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true); +var hostname = urlParts.hostname; +var protocol = urlParts.protocol; + // 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: protocol, + hostname: hostname, + port: (urlParts.port === "0") ? window.location.port : urlParts.port, // Hardcoded in WebpackDevServer pathname: '/sockjs-node' }));