From 6ca3a6fecb9b1a4ae85dd0c6db30b0eb1b19c276 Mon Sep 17 00:00:00 2001 From: Danila Rodichkin Date: Wed, 17 Jan 2024 14:53:14 +0300 Subject: [PATCH] fix(nuxt): don't share object between raw cookie and cookie ref (#25255) --- packages/nuxt/src/app/composables/cookie.ts | 4 +++- test/basic.test.ts | 2 ++ test/fixtures/basic/pages/cookies.vue | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) 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 }}
-