Skip to content

Commit d9cd8e1

Browse files
authored
fix: disable socket injection when hot & liveReload are disabled (V4) (#2601)
1 parent 76bcd84 commit d9cd8e1

File tree

7 files changed

+582
-47
lines changed

7 files changed

+582
-47
lines changed

client-src/default/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const onSocketMessage = {
137137
},
138138
close() {
139139
log.error('[WDS] Disconnected!');
140+
140141
sendMessage('Close');
141142
},
142143
};

lib/Server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,9 @@ class Server {
678678
this.hostname = hostname;
679679

680680
return this.listeningApp.listen(port, hostname, (err) => {
681-
this.createSocketServer();
681+
if (this.options.hot || this.options.liveReload) {
682+
this.createSocketServer();
683+
}
682684

683685
if (this.options.bonjour) {
684686
runBonjour(this.options);
@@ -923,7 +925,7 @@ class Server {
923925

924926
const watcher = chokidar.watch(watchPath, watchOptions);
925927
// disabling refreshing on changing the content
926-
if (this.options.liveReload !== false) {
928+
if (this.options.liveReload) {
927929
watcher.on('change', () => {
928930
this.sockWrite(this.sockets, 'content-changed');
929931
});

test/client/__snapshots__/index.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`index should run onSocketMessage.close (hot enabled) 1`] = `"[WDS] Disconnected!"`;
4+
5+
exports[`index should run onSocketMessage.close (hot enabled) 2`] = `"Close"`;
6+
7+
exports[`index should run onSocketMessage.close (liveReload enabled) 1`] = `"[WDS] Disconnected!"`;
8+
9+
exports[`index should run onSocketMessage.close (liveReload enabled) 2`] = `"Close"`;
10+
311
exports[`index should run onSocketMessage.close 1`] = `"[WDS] Disconnected!"`;
412

513
exports[`index should run onSocketMessage.close 2`] = `"Close"`;

test/client/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,20 @@ describe('index', () => {
214214
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
215215
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
216216
});
217+
218+
test('should run onSocketMessage.close (hot enabled)', () => {
219+
// enabling hot
220+
onSocketMessage.hot();
221+
onSocketMessage.close();
222+
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
223+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
224+
});
225+
226+
test('should run onSocketMessage.close (liveReload enabled)', () => {
227+
// enabling liveReload
228+
onSocketMessage.liveReload();
229+
onSocketMessage.close();
230+
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
231+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
232+
});
217233
});

test/e2e/ClientOptions.test.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ describe('Client console.log', () => {
365365
port: port2,
366366
host: '0.0.0.0',
367367
};
368+
const transportModes = [
369+
{},
370+
{ transportMode: 'sockjs' },
371+
{ transportMode: 'ws' },
372+
];
373+
368374
const cases = [
369375
{
370376
title: 'hot disabled',
@@ -390,6 +396,13 @@ describe('Client console.log', () => {
390396
liveReload: true,
391397
},
392398
},
399+
{
400+
title: 'liveReload & hot are disabled',
401+
options: {
402+
liveReload: false,
403+
hot: false,
404+
},
405+
},
393406
// TODO: make clientLogLevel work as expected for HMR logs
394407
{
395408
title: 'clientLogLevel is silent',
@@ -399,48 +412,29 @@ describe('Client console.log', () => {
399412
},
400413
];
401414

402-
cases.forEach(({ title, options }) => {
403-
it(title, (done) => {
404-
const res = [];
415+
transportModes.forEach(async (mode) => {
416+
await cases.forEach(async ({ title, options }) => {
417+
title += ` (${
418+
Object.keys(mode).length ? mode.transportMode : 'default'
419+
})`;
420+
options = { ...mode, ...options };
405421
const testOptions = Object.assign({}, baseOptions, options);
406-
407-
// TODO: use async/await when Node.js v6 support is dropped
408-
Promise.resolve()
409-
.then(() => {
410-
return new Promise((resolve) => {
411-
testServer.startAwaitingCompilation(config, testOptions, resolve);
412-
});
413-
})
414-
.then(() => {
415-
// make sure the previous Promise is not passing along strange arguments to runBrowser
416-
return runBrowser();
417-
})
418-
.then(({ page, browser }) => {
419-
return new Promise((resolve) => {
420-
page.goto(`http://localhost:${port2}/main`);
421-
page.on('console', ({ _text }) => {
422-
res.push(_text);
423-
});
424-
// wait for load before closing the browser
425-
page.waitForNavigation({ waitUntil: 'load' }).then(() => {
426-
page.waitFor(beforeBrowserCloseDelay).then(() => {
427-
browser.close().then(() => {
428-
resolve();
429-
});
430-
});
431-
});
432-
});
433-
})
434-
.then(() => {
435-
return new Promise((resolve) => {
436-
testServer.close(resolve);
437-
});
438-
})
439-
.then(() => {
440-
// Order doesn't matter, maybe we should improve that in future
441-
expect(res.sort()).toMatchSnapshot();
442-
done();
422+
await it(title, async (done) => {
423+
await testServer.startAwaitingCompilation(config, testOptions);
424+
const res = [];
425+
const { page, browser } = await runBrowser();
426+
page.goto(`http://localhost:${port2}/main`);
427+
page.on('console', ({ _text }) => {
428+
res.push(_text);
443429
});
430+
// wait for load before closing the browser
431+
await page.waitForNavigation({ waitUntil: 'load' });
432+
await page.waitFor(beforeBrowserCloseDelay);
433+
await browser.close();
434+
// Order doesn't matter, maybe we should improve that in future
435+
await expect(res.sort()).toMatchSnapshot();
436+
await testServer.close(done);
437+
});
444438
});
445439
});
446440
});

0 commit comments

Comments
 (0)