Skip to content

Commit

Permalink
fix(nuxt): handle failure creating BroadcastChannel (#26340)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 18, 2024
1 parent 9b9558b commit 2c0fc3a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2c0fc3a

Please sign in to comment.