Skip to content

Commit fd4cc7d

Browse files
fix: make BroadcastChannel a singleton (#10762)
to prevent unnecessary session requests
1 parent ba1304c commit fd4cc7d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

packages/next-auth/src/react.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,22 @@ export const __NEXTAUTH: AuthClientConfig = {
6969
_getSession: () => {},
7070
}
7171

72+
let broadcastChannel: BroadcastChannel | null = null
73+
7274
function broadcast() {
73-
if (typeof BroadcastChannel !== "undefined") {
74-
return new BroadcastChannel("next-auth")
75+
if (typeof BroadcastChannel === "undefined") {
76+
return {
77+
postMessage: () => {},
78+
addEventListener: () => {},
79+
removeEventListener: () => {},
80+
}
7581
}
76-
return {
77-
postMessage: () => {},
78-
addEventListener: () => {},
79-
removeEventListener: () => {},
82+
83+
if (broadcastChannel === null) {
84+
broadcastChannel = new BroadcastChannel("next-auth")
8085
}
86+
87+
return broadcastChannel
8188
}
8289

8390
// TODO:

0 commit comments

Comments
 (0)