fix(nuxt): clear cookie BroadcastChannel when scope is disposed (#23664)

This commit is contained in:
Alex Liu 2023-10-14 04:10:34 +08:00 committed by GitHub
parent 1d43cdc908
commit 5c7cca170a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import type { Ref } from 'vue' 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 type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es'
import { parse, serialize } from 'cookie-es' import { parse, serialize } from 'cookie-es'
import { deleteCookie, getCookie, getRequestHeader, setCookie } from 'h3' import { deleteCookie, getCookie, getRequestHeader, setCookie } from 'h3'
@ -35,7 +35,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
if (import.meta.client) { if (import.meta.client) {
const channel = typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(`nuxt:cookies:${name}`) const channel = typeof BroadcastChannel === 'undefined' ? null : new BroadcastChannel(`nuxt:cookies:${name}`)
if (getCurrentInstance()) { onUnmounted(() => { channel?.close() }) } if (getCurrentScope()) { onScopeDispose(() => { channel?.close() }) }
const callback = () => { const callback = () => {
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)