From 5c7cca170a85b0584b4091e7e7e461b4800c0e36 Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Sat, 14 Oct 2023 04:10:34 +0800 Subject: [PATCH] fix(nuxt): clear cookie BroadcastChannel when scope is disposed (#23664) --- packages/nuxt/src/app/composables/cookie.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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)