From 34adac661de1935eda63dab516e103d8196e3a85 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 16 Oct 2023 14:36:30 +0100 Subject: [PATCH] fix(nuxt): write cookie values before navigating away (#23697) --- packages/nuxt/src/app/composables/cookie.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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