Skip to content

Commit 3da8214

Browse files
feat: allow to set client.webSocketURL.protocol
1 parent 7e86922 commit 3da8214

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

client-src/utils/createSocketURL.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ function createSocketURL(parsedURL) {
2424
hostname = self.location.hostname;
2525
}
2626

27+
if (protocol === 'auto:') {
28+
protocol = self.location.protocol;
29+
}
30+
2731
// `hostname` can be empty when the script path is relative. In that case, specifying a protocol would result in an invalid URL.
28-
// When https is used in the app, secure websockets are always necessary because the browser doesn't accept non-secure websockets.
32+
// When https is used in the app, secure web sockets are always necessary because the browser doesn't accept non-secure web sockets.
2933
if (hostname && isInAddrAny && self.location.protocol === 'https:') {
3034
protocol = self.location.protocol;
3135
}

lib/options.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,18 @@
290290
"type": "object",
291291
"additionalProperties": false,
292292
"properties": {
293+
"protocol": {
294+
"anyOf": [
295+
{
296+
"enum": ["auto"]
297+
},
298+
{
299+
"type": "string",
300+
"minLength": 1
301+
}
302+
],
303+
"description": "Tells clients connected to devServer to use the provided protocol."
304+
},
293305
"host": {
294306
"type": "string",
295307
"minLength": 1,

lib/utils/DevServerPlugin.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ class DevServerPlugin {
2828
apply(compiler) {
2929
const { options } = this;
3030

31-
/** @type {"ws" | "wss"} */
32-
const protocol = options.https ? 'wss' : 'ws';
31+
/** @type {"ws:" | "wss:" | "http:" | "https:" | "auto:"} */
32+
let protocol;
33+
34+
// We are proxying dev server and need to specify custom `host`
35+
if (typeof options.client.webSocketURL.protocol !== 'undefined') {
36+
protocol = options.client.webSocketURL.protocol;
37+
} else {
38+
protocol = options.https ? 'wss:' : 'ws:';
39+
}
3340

3441
/** @type {string} */
3542
let host;
@@ -111,7 +118,7 @@ class DevServerPlugin {
111118

112119
const webSocketURL = encodeURIComponent(
113120
new URL(
114-
`${protocol}://${ipaddr.IPv6.isIPv6(host) ? `[${host}]` : host}${
121+
`${protocol}//${ipaddr.IPv6.isIPv6(host) ? `[${host}]` : host}${
115122
port ? `:${port}` : ''
116123
}${path || '/'}${
117124
Object.keys(searchParams).length > 0

lib/utils/normalizeOptions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ function normalizeOptions(compiler, options, logger) {
120120
const parsedURL = new URL(options.client.webSocketURL);
121121

122122
options.client.webSocketURL = {
123+
protocol: parsedURL.protocol,
123124
host: parsedURL.hostname,
124-
port: Number(parsedURL.port),
125+
port: parsedURL.port.length > 0 ? Number(parsedURL.port) : '',
125126
path: parsedURL.pathname,
126127
};
127128
} else if (typeof options.client.webSocketURL.port === 'string') {

0 commit comments

Comments
 (0)