fix(nuxt): write cookie values before navigating away (#23697)

This commit is contained in:
Daniel Roe 2023-10-16 14:36:30 +01:00 committed by GitHub
parent 24b629e82e
commit 34adac661d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,8 +35,6 @@ 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 (getCurrentScope()) { onScopeDispose(() => { channel?.close() }) }
const callback = () => { const callback = () => {
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)
channel?.postMessage(opts.encode(cookie.value as T)) channel?.postMessage(opts.encode(cookie.value as T))
@ -44,6 +42,14 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
let watchPaused = false let watchPaused = false
if (getCurrentScope()) {
onScopeDispose(() => {
watchPaused = true
callback()
channel?.close()
})
}
if (channel) { if (channel) {
channel.onmessage = (event) => { channel.onmessage = (event) => {
watchPaused = true watchPaused = true