Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ hotClient.useCustomOverlay({
// Connect manually when `autoConnect=false`. Accepts the same option keys as
// the query-string API above.
hotClient.setOptionsAndConnect({ path: "/__hmr" });

// Close the SSE connection and stop reconnecting (e.g. before tearing the
// page down). A later `setOptionsAndConnect` call opens a fresh connection.
hotClient.disconnect();
```

## API
Expand Down
25 changes: 24 additions & 1 deletion client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,17 @@ function createEventSourceWrapper() {
}
};

const handleDisconnect = () => {
/**
* Close the connection and stop the activity timer without scheduling a
* reconnection.
*/
const close = () => {
clearInterval(timer);
source.close();
};

const handleDisconnect = () => {
close();
setTimeout(init, /** @type {number} */ (options.timeout));
};

Expand Down Expand Up @@ -135,6 +143,7 @@ function createEventSourceWrapper() {
addMessageListener(fn) {
listeners.push(fn);
},
close,
};
}

Expand Down Expand Up @@ -180,6 +189,20 @@ export function setOptionsAndConnect(overrides) {
connect();
}

/**
* Close the SSE connection for the current path and stop reconnecting. A
* later `setOptionsAndConnect` call opens a fresh connection.
*/
export function disconnect() {
const path = /** @type {string} */ (options.path);
const wrappers = window[WRAPPER_KEY];

if (wrappers && wrappers[path]) {
wrappers[path].close();
delete wrappers[path];
}
}

// eslint-disable-next-line jsdoc/reject-any-type
/** @typedef {any} EXPECTED_ANY */

Expand Down
26 changes: 26 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,32 @@ describe("client", () => {
jest.advanceTimersByTime(20 * 1000);
expect(EventSourceStub.instances).toHaveLength(2);
});

it("disconnect() closes the connection and does not reconnect", () => {
jest.useFakeTimers({ doNotFake: ["nextTick"] });
delete globalThis.__wdmEventSourceWrapper;
EventSourceStub.instances.length = 0;
client = loadClient();

const [first] = EventSourceStub.instances;
client.disconnect();

expect(first.closed).toBe(true);
// Neither the watchdog nor a reconnect timer may re-open it.
jest.advanceTimersByTime(60 * 1000);
expect(EventSourceStub.instances).toHaveLength(1);
});

it("disconnect() drops the cached wrapper so a new connect starts fresh", () => {
client.disconnect();
expect(
globalThis.__wdmEventSourceWrapper["/__webpack_hmr"],
).toBeUndefined();

client.setOptionsAndConnect({});
expect(EventSourceStub.instances).toHaveLength(2);
expect(EventSourceStub.lastInstance().closed).toBe(false);
});
});

describe("with logging option", () => {
Expand Down
Loading