From 640abfcb46320126e56fe15c799c1b0a4e9c0354 Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Tue, 26 Apr 2022 00:03:03 +0200 Subject: [PATCH 1/3] fix: make current script detection more robust on edge cases --- sockets/utils/getCurrentScriptSource.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sockets/utils/getCurrentScriptSource.js b/sockets/utils/getCurrentScriptSource.js index 91301b6d..37b61389 100644 --- a/sockets/utils/getCurrentScriptSource.js +++ b/sockets/utils/getCurrentScriptSource.js @@ -5,16 +5,20 @@ 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'); } From 539158f09a4c311a3abc9842ecf37012b5b81a2c Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Tue, 26 Apr 2022 00:06:18 +0200 Subject: [PATCH 2/3] docs: update docs for WDS socket options --- docs/API.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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` From 6e1f515d18e643460a82dffa402fc424fe2f47df Mon Sep 17 00:00:00 2001 From: Michael Mok Date: Tue, 26 Apr 2022 00:07:40 +0200 Subject: [PATCH 3/3] chore: format --- sockets/utils/getCurrentScriptSource.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sockets/utils/getCurrentScriptSource.js b/sockets/utils/getCurrentScriptSource.js index 37b61389..ed584352 100644 --- a/sockets/utils/getCurrentScriptSource.js +++ b/sockets/utils/getCurrentScriptSource.js @@ -5,7 +5,7 @@ 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 ("currentScript" in document) { + 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. @@ -15,9 +15,12 @@ function getCurrentScriptSource() { // 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'); - }); + 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');