fix(nuxt): handle failure creating BroadcastChannel (#26340)

This commit is contained in:
Daniel Roe 2024-03-18 14:15:10 +00:00 committed by GitHub
parent 9b9558bcf9
commit 2c0fc3a15e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -63,7 +63,15 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
}
if (import.meta.client) {
const channel = store || typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(`nuxt:cookies:${name}`)
let channel: null | BroadcastChannel = null
try {
if (!store && typeof BroadcastChannel !== 'undefined') {
channel = new BroadcastChannel(`nuxt:cookies:${name}`)
}
} catch {
// BroadcastChannel will fail in certain situations when cookies are disabled
// or running in an iframe: see https://github.com/nuxt/nuxt/issues/26338
}
const callback = () => {
if (opts.readonly || isEqual(cookie.value, cookies[name])) { return }
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)