diff --git a/docs/API.md b/docs/API.md index 6e86d0e0..bc0b0610 100644 --- a/docs/API.md +++ b/docs/API.md @@ -226,7 +226,7 @@ You can reference implementations inside the [`sockets`](https://github.com/pmmm #### `sockHost` -Default: `window.location.hostname` +Default: Parsed from current URL Type: `string` @@ -237,7 +237,7 @@ Useful if you set `devServer.sockHost` to something other than `window.location. #### `sockPort` -Default: `window.location.port` +Default: Parsed from current URL Type: `number` @@ -248,7 +248,7 @@ Useful if you set `devServer.sockPort` to something other than `window.location. #### `sockPath` -Default: `/sockjs-node` +Default: `/ws` for WDS v4, `/sockjs-node` for WDS v3 Type: `string` diff --git a/sockets/utils/getCurrentScriptSource.js b/sockets/utils/getCurrentScriptSource.js index 91301b6d..ed584352 100644 --- a/sockets/utils/getCurrentScriptSource.js +++ b/sockets/utils/getCurrentScriptSource.js @@ -5,16 +5,23 @@ function getCurrentScriptSource() { // `document.currentScript` is the most accurate way to get the current running script, // but is not supported in all browsers (most notably, IE). - if (document.currentScript) { + if ('currentScript' in document) { + // In some cases, `document.currentScript` would be `null` even if the browser supports it: + // e.g. asynchronous chunks on Firefox. + // We should not fallback to the list-approach as it would not be safe. + if (document.currentScript == null) return; return document.currentScript.getAttribute('src'); } - - // Fallback to getting all scripts running in the document. - const scriptElements = document.scripts || []; - const scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (elem) { - return elem.getAttribute('src'); - }); - if (scriptElementsWithSrc.length) { + // Fallback to getting all scripts running in the document, + // and finding the last one injected. + else { + const scriptElementsWithSrc = Array.prototype.filter.call( + document.scripts || [], + function (elem) { + return elem.getAttribute('src'); + } + ); + if (!scriptElementsWithSrc.length) return; const currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1]; return currentScript.getAttribute('src'); }