Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(devtools): avoid polluting user console #5936

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/shiny-cougars-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/devtools-shared": patch
---

chore: prevent websocket closing errors in console

When `<DevtoolsProvider />` component is mounted in apps with React's strict mode, it will try to initialize the websocket connection twice and first one will be closed immediately before the connection is established. This PR will delay closing the websocket connection until it's established properly to prevent these errors from appearing in the console.
8 changes: 8 additions & 0 deletions .changeset/wise-dots-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@refinedev/devtools-server": patch
"@refinedev/devtools-ui": patch
---

fix: remove annoying auth error at initial project loads

When users create a new project or their devtools token expires, their console is polluted with network errors due to missing authentication. This PR removes these errors by handling auth requests in a user-friendly way.
11 changes: 10 additions & 1 deletion packages/devtools-server/src/serve-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,16 @@ export const serveProxy = async (app: Express) => {
saveAuth(token, jwt);
})(proxyRes, req, res);
}
res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);

if (proxyRes.statusCode === 401) {
res.writeHead(200, {
...proxyRes.headers,
"Access-Control-Expose-Headers": `Refine-Is-Authenticated, ${proxyRes.headers["Access-Control-Expose-Headers"]}`,
});
} else {
res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
}

proxyRes.pipe(res, { end: true });
},
});
Expand Down
11 changes: 10 additions & 1 deletion packages/devtools-shared/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ export const DevToolsContextProvider: React.FC<Props> = ({
return () => {
unsubscribe();

wsInstance.close(1000, window.location.origin);
// In strict mode, the WebSocket instance might not be connected yet
// so we need to wait for it to connect before closing it
// otherwise it will log an unnecessary error in the console
if (wsInstance.readyState === WebSocket.CONNECTING) {
wsInstance.addEventListener("open", () => {
wsInstance.close(1000, window.location.origin);
});
} else {
wsInstance.close(1000, window.location.origin);
}
};
}, []);

Expand Down
5 changes: 3 additions & 2 deletions packages/devtools-ui/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { ory } from "./ory";

export const isAuthenticated = async () => {
try {
await ory.toSession();
return true;
const response = await ory.toSession();
const headerAuth = Boolean(response.headers["Refine-Is-Authenticated"]);
return headerAuth;
} catch (error: any) {
return false;
}
Expand Down