It appears that there is no official solution available at the moment. I have submitted a request on the repository: PLEASE UPVOTE it.
Meanwhile, I have come up with some rather unattractive workarounds that involve patching the hot reload script.
These workarounds need to be executed before the hot reload script (
/_framework/aspnetcore-browser-refresh.js
) loads.
Temporary Workaround 1
class DummyWebSocket {
constructor(url, protocol) { }
addEventListener(eventName, callback) {
if (eventName === 'close')
callback(new ErrorEvent(''));
}
removeEventListener(eventName, callback) { }
}
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
if (args[0].startsWith('wss://localhost:'))
return new DummyWebSocket(...args);
else
return new nativeWebSocket(...args);
};
With this workaround, only undefined
is logged in the devtools console.
Note that while this reduces the noise of two red errors, it may not be foolproof and could fail if the underlying script changes.
Temporary Workaround 2
This workaround completely suppresses logging but aggressively patches console.debug
.
class DummyWebSocket {
constructor(url, protocol) { }
addEventListener(eventName, callback) {
if (eventName === 'close')
callback(); // <----
}
removeEventListener(eventName, callback) { }
}
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
if (args[0].startsWith('wss://localhost:'))
return new DummyWebSocket(...args);
else
return new nativeWebSocket(...args);
};
const nativeConsoleDebug = window.console.debug; // <----
window.console.debug = function(...data) {
if (data[0] === 'WebSocket failed to connect.')
return;
else
nativeConsoleDebug(...data);
}
If you have a more effective workaround, please share it so I can accept your solution.