diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index 83f07b5972..16d703b52a 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -1,5 +1,5 @@ import type { Ref } from 'vue' -import { getCurrentInstance, nextTick, onUnmounted, ref, watch } from 'vue' +import { getCurrentScope, nextTick, onScopeDispose, ref, watch } from 'vue' import type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es' import { parse, serialize } from 'cookie-es' import { deleteCookie, getCookie, getRequestHeader, setCookie } from 'h3' @@ -35,7 +35,7 @@ export function useCookie (name: string, _opts?: if (import.meta.client) { const channel = typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(`nuxt:cookies:${name}`) - if (getCurrentInstance()) { onUnmounted(() => { channel?.close() }) } + if (getCurrentScope()) { onScopeDispose(() => { channel?.close() }) } const callback = () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)