Skip to content

Commit e142fbd

Browse files
resolve socksjs websocket host/port at runtime instead of assuming window.location
1 parent d72c749 commit e142fbd

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

packages/react-dev-utils/webpackHotDevClient.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,31 @@ function showErrorOverlay(message) {
123123
});
124124
}
125125

126+
function getCurrentScriptSource() {
127+
// `document.currentScript` is the most accurate way to find the current script,
128+
// but is not supported in all browsers.
129+
if(document.currentScript)
130+
return document.currentScript.getAttribute("src");
131+
// Fall back to getting all scripts in the document.
132+
var scriptElements = document.scripts || [];
133+
var currentScript = scriptElements[scriptElements.length - 1];
134+
if(currentScript)
135+
return currentScript.getAttribute("src");
136+
}
137+
138+
139+
var scriptHost = getCurrentScriptSource();
140+
scriptHost = scriptHost.replace(/\/[^\/]+$/, "");
141+
142+
var urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
143+
var hostname = urlParts.hostname;
144+
var protocol = urlParts.protocol;
145+
126146
// Connect to WebpackDevServer via a socket.
127147
var connection = new SockJS(url.format({
128-
protocol: window.location.protocol,
129-
hostname: window.location.hostname,
130-
port: window.location.port,
148+
protocol: protocol,
149+
hostname: hostname,
150+
port: (urlParts.port === "0") ? window.location.port : urlParts.port,
131151
// Hardcoded in WebpackDevServer
132152
pathname: '/sockjs-node'
133153
}));

0 commit comments

Comments
 (0)