Skip to content

Commit 02e6fba

Browse files
authored
Fix intermittent issue where a page refresh or navigation might mistakenly connect but show no data (#1378)
1 parent 7267351 commit 02e6fba

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

.changeset/dirty-rules-deny.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+
Fix intermittent issue where data would not show up in the devtools after refreshing the page while the Apollo devtools panel is open.

src/application/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export const App = () => {
130130
const dismiss = BannerAlert.show(ALERT_CONFIGS[state]);
131131

132132
if (state === "connected") {
133+
setClientNotFoundModalOpen(false);
133134
timeout = setTimeout(dismiss, 2500);
134135
}
135136

src/application/machines.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ export function createDevtoolsMachine({ actions }: { actions: Actions }) {
8787
timeout: "timedout",
8888
clientNotFound: "notFound",
8989
},
90-
entry: ["unsubscribeFromAll", "connectToClient"],
90+
entry: ["unsubscribeFromAll"],
9191
},
9292
timedout: {},
9393
notFound: {
9494
on: {
9595
retry: "retrying",
96+
connect: "connected",
9697
},
9798
entry: "unsubscribeFromAll",
9899
},

src/extension/devtools/devtools.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const devtoolsMachine = interpret(
4040

4141
let panelHidden = true;
4242
let connectTimeoutId: NodeJS.Timeout;
43+
let disconnectTimeoutId: NodeJS.Timeout;
4344

4445
const port = browser.runtime.connect({
4546
name: inspectedTabId.toString(),
@@ -50,6 +51,12 @@ const rpcClient = createRpcClient<DevtoolsRPCMessage>(
5051
createPortMessageAdapter(port)
5152
);
5253

54+
devtoolsMachine.subscribe(({ value }) => {
55+
if (value === "connected") {
56+
clearTimeout(disconnectTimeoutId);
57+
}
58+
});
59+
5360
// In case we can't connect to the tab, we should at least show something to the
5461
// user when we've attempted to connect a max number of times.
5562
function startConnectTimeout(attempts = 0) {
@@ -73,12 +80,17 @@ clientPort.on("connectToDevtools", (message) => {
7380
});
7481
});
7582

76-
clientPort.on("connectToClientTimeout", () => {
77-
devtoolsMachine.send("timeout");
83+
clientPort.on("registerClient", (message) => {
84+
devtoolsMachine.send({ type: "connect", clientContext: message.payload });
7885
});
7986

8087
clientPort.on("disconnectFromDevtools", () => {
88+
clearTimeout(disconnectTimeoutId);
8189
devtoolsMachine.send("disconnect");
90+
91+
disconnectTimeoutId = setTimeout(() => {
92+
devtoolsMachine.send("clientNotFound");
93+
}, 10_000);
8294
});
8395

8496
clientPort.on("clientNotFound", () => {

src/extension/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ type ExplorerSubscriptionTerminationMessage = {
8282
};
8383

8484
export type ClientMessage =
85+
| { type: "registerClient"; payload: ClientContext }
8586
| { type: "clientNotFound" }
8687
| { type: "connectToClient" }
87-
| { type: "connectToClientTimeout" }
8888
| {
8989
type: "connectToDevtools";
9090
payload: ClientContext;

src/extension/tab/hook.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ function registerClient(client: ApolloClient<any>) {
268268
if (!knownClients.has(client)) {
269269
knownClients.add(client);
270270
watchForClientTermination(client);
271+
tab.send({ type: "registerClient", payload: getClientData() });
271272
}
272273

273274
hook.ApolloClient = client;
@@ -283,9 +284,6 @@ function registerClient(client: ApolloClient<any>) {
283284
// });
284285

285286
clearInterval(interval);
286-
// incase initial update was missed because the client wasn't ready, send the create devtools event.
287-
// devtools checks to see if it's already created, so this won't create duplicate tabs
288-
sendHookDataToDevTools("connectToDevtools");
289287
loadErrorCodes(rpcClient, client.version);
290288
}
291289

src/extension/tab/tab.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ devtools.forward("connectToClient", tab);
2323
devtools.forward("explorerSubscriptionTermination", tab);
2424
devtools.forward("explorerRequest", tab);
2525

26+
tab.forward("registerClient", devtools);
2627
tab.forward("clientNotFound", devtools);
2728
tab.forward("connectToDevtools", devtools);
2829
tab.forward("disconnectFromDevtools", devtools);

0 commit comments

Comments
 (0)