Skip to content

fix: always check typeof BroadcastChannel #12937

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

Merged
merged 5 commits into from
May 13, 2025
Merged
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
20 changes: 13 additions & 7 deletions packages/next-auth/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,27 @@ export const __NEXTAUTH: AuthClientConfig = {
_getSession: () => {},
}

// https://github.com/nextauthjs/next-auth/pull/10762
let broadcastChannel: BroadcastChannel | null = null

function getNewBroadcastChannel() {
return new BroadcastChannel("next-auth")
}

function broadcast() {
if (typeof BroadcastChannel === "undefined") {
return {
postMessage: () => {},
addEventListener: () => {},
removeEventListener: () => {},
}
name: "next-auth",
onmessage: null,
onmessageerror: null,
close: () => {},
dispatchEvent: () => false,
} satisfies BroadcastChannel
}

return new BroadcastChannel("next-auth")
}

function broadcast() {
if (broadcastChannel === null) {
broadcastChannel = getNewBroadcastChannel()
}
Expand Down Expand Up @@ -180,8 +186,8 @@ export async function getSession(params?: GetSessionParams) {
params
)
if (params?.broadcast ?? true) {
const broadcastChannel = getNewBroadcastChannel()
broadcastChannel.postMessage({
// https://github.com/nextauthjs/next-auth/pull/11470
getNewBroadcastChannel().postMessage({
event: "session",
data: { trigger: "getSession" },
})
Expand Down
Loading