Skip to content

Commit 6d880dd

Browse files
authored
Use browser.devtools.network.onNavigated to detect page navigation (#1391)
1 parent 526a5d2 commit 6d880dd

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

.changeset/wise-spoons-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"apollo-client-devtools": patch
3+
---
4+
5+
Use browser.devtools.network to detect page navigation

src/extension/devtools/devtools.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ function connectToClient() {
5656
startConnectTimeout();
5757
}
5858

59+
function disconnectFromDevtools() {
60+
devtoolsMachine.send("disconnect");
61+
startConnectTimeout();
62+
}
63+
5964
function startConnectTimeout() {
6065
clearTimeout(connectTimeoutId);
6166

@@ -75,10 +80,7 @@ clientPort.on("registerClient", (message) => {
7580
devtoolsMachine.send({ type: "connect", clientContext: message.payload });
7681
});
7782

78-
clientPort.on("disconnectFromDevtools", () => {
79-
devtoolsMachine.send("disconnect");
80-
startConnectTimeout();
81-
});
83+
clientPort.on("clientTerminated", disconnectFromDevtools);
8284

8385
connectToClient();
8486

@@ -161,3 +163,5 @@ async function createDevtoolsPanel() {
161163
}
162164

163165
createDevtoolsPanel();
166+
167+
browser.devtools.network.onNavigated.addListener(disconnectFromDevtools);

src/extension/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type ClientMessage =
8888
type: "connectToDevtools";
8989
payload: ClientContext;
9090
}
91-
| { type: "disconnectFromDevtools" }
91+
| { type: "clientTerminated" }
9292
| ExplorerRequestMessage
9393
| ExplorerResponseMessage
9494
| ExplorerSubscriptionTerminationMessage;

src/extension/tab/hook.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ Object.defineProperty(window, "__APOLLO_DEVTOOLS_GLOBAL_HOOK__", {
8686
configurable: true,
8787
});
8888

89-
// Listen for tab refreshes
90-
window.onbeforeunload = () => {
91-
tab.send({ type: "disconnectFromDevtools" });
92-
};
93-
9489
function getClientData() {
9590
// We need to JSON stringify the data here in case the cache contains
9691
// references to irregular data such as `URL` instances which are not
@@ -228,7 +223,7 @@ function watchForClientTermination(client: ApolloClient<any>) {
228223
hook.ApolloClient = undefined;
229224
}
230225

231-
tab.send({ type: "disconnectFromDevtools" });
226+
tab.send({ type: "clientTerminated" });
232227
originalStop.call(client);
233228
};
234229
}

src/extension/tab/tab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ devtools.forward("explorerRequest", tab);
2525

2626
tab.forward("registerClient", devtools);
2727
tab.forward("connectToDevtools", devtools);
28-
tab.forward("disconnectFromDevtools", devtools);
28+
tab.forward("clientTerminated", devtools);
2929
tab.forward("explorerResponse", devtools);
3030

3131
// We run the hook.js script on the page as a content script in Manifest v3

0 commit comments

Comments
 (0)