Skip to content

Commit

Permalink
resolve socksjs websocket host/port at runtime instead of assuming wi…
Browse files Browse the repository at this point in the history
…ndow.location
  • Loading branch information
mcallistersean committed Sep 28, 2016
1 parent d72c749 commit e142fbd
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}));
Expand Down

0 comments on commit e142fbd

Please sign in to comment.