From 2c0fc3a15e241fc53b97632b96f5466e87092345 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 18 Mar 2024 14:15:10 +0000 Subject: [PATCH] fix(nuxt): handle failure creating BroadcastChannel (#26340) --- packages/nuxt/src/app/composables/cookie.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index 9293e217e8..37156c81dd 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)