diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index 16d703b52a..be545fd861 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -35,8 +35,6 @@ export function useCookie (name: string, _opts?: if (import.meta.client) { const channel = typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(`nuxt:cookies:${name}`) - if (getCurrentScope()) { onScopeDispose(() => { channel?.close() }) } - const callback = () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) channel?.postMessage(opts.encode(cookie.value as T)) @@ -44,6 +42,14 @@ export function useCookie (name: string, _opts?: let watchPaused = false + if (getCurrentScope()) { + onScopeDispose(() => { + watchPaused = true + callback() + channel?.close() + }) + } + if (channel) { channel.onmessage = (event) => { watchPaused = true