diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index 9293e217e..37156c81d 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -63,7 +63,15 @@ export function useCookie (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)