diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index b030bfcf6f..de05925777 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -61,6 +61,8 @@ export function useCookie (name: string, _opts?: const callback = () => { if (opts.readonly || isEqual(cookie.value, cookies[name])) { return } writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) + + cookies[name] = klona(cookie.value) channel?.postMessage(opts.encode(cookie.value as T)) } @@ -107,7 +109,7 @@ export function useCookie (name: string, _opts?: return cookie as CookieRef } -function readRawCookies (opts: CookieOptions = {}): Record | undefined { +function readRawCookies (opts: CookieOptions = {}): Record | undefined { if (import.meta.server) { return parse(getRequestHeader(useRequestEvent(), 'cookie') || '', opts) } else if (import.meta.client) { diff --git a/test/basic.test.ts b/test/basic.test.ts index 473e74c235..287717ce2b 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -501,6 +501,8 @@ describe('nuxt composables', () => { expect(await extractCookie()).toEqual({ foo: 'bar' }) await page.getByRole('button').click() expect(await extractCookie()).toEqual({ foo: 'baz' }) + await page.getByRole('button').click() + expect(await extractCookie()).toEqual({ foo: 'bar' }) await page.close() }) }) diff --git a/test/fixtures/basic/pages/cookies.vue b/test/fixtures/basic/pages/cookies.vue index 7c14eed8ba..1875097b11 100644 --- a/test/fixtures/basic/pages/cookies.vue +++ b/test/fixtures/basic/pages/cookies.vue @@ -20,7 +20,7 @@ const objectCookie = useCookie('browser-object-default', {
cookies testing page
{{ objectCookie }}
-