Skip to content

Commit f770b90

Browse files
authoredSep 28, 2022
fix(react): safe use of localStorage API (#5444)
fix: safe use of localstorage Co-authored-by: Etienne <>
1 parent 87f4786 commit f770b90

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎packages/next-auth/src/client/_utils.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,18 @@ export function BroadcastChannel(name = "nextauth.message") {
9494
/** Notify other tabs/windows. */
9595
post(message: Record<string, unknown>) {
9696
if (typeof window === "undefined") return
97-
localStorage.setItem(
98-
name,
99-
JSON.stringify({ ...message, timestamp: now() })
100-
)
97+
try {
98+
localStorage.setItem(
99+
name,
100+
JSON.stringify({ ...message, timestamp: now() })
101+
)
102+
} catch {
103+
/**
104+
* The localStorage API isn't always available.
105+
* It won't work in private mode prior to Safari 11 for example.
106+
* Notifications are simply dropped if an error is encountered.
107+
*/
108+
}
101109
},
102110
}
103111
}

0 commit comments

Comments
 (0)
Please sign in to comment.