From 876d910c3fc27788d18da77a756c80e76f2918fc Mon Sep 17 00:00:00 2001 From: Enkot Date: Sun, 4 Feb 2024 01:15:26 +0200 Subject: [PATCH] fix(nuxt): broadcast cookie change in correct format (#25598) --- packages/nuxt/src/app/composables/cookie.ts | 2 +- test/basic.test.ts | 6 ++++-- test/fixtures/basic/pages/cookies.vue | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index be64a70364..09015f8544 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -69,7 +69,7 @@ export function useCookie (name: string, _opts?: writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) cookies[name] = klona(cookie.value) - channel?.postMessage(opts.encode(cookie.value as T)) + channel?.postMessage({ value: opts.encode(cookie.value as T) }) } const handleChange = (data: { value?: any, refresh?: boolean }) => { diff --git a/test/basic.test.ts b/test/basic.test.ts index 225af98542..abaa830c4e 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -490,11 +490,13 @@ describe('nuxt composables', () => { expect(await extractCookie()).toEqual({ foo: 'bar' }) await page.getByText('Change cookie').click() expect(await extractCookie()).toEqual({ foo: 'baz' }) + let text = await page.innerText('pre') + expect(text).toContain('baz') await page.getByText('Change cookie').click() expect(await extractCookie()).toEqual({ foo: 'bar' }) - await page.evaluate(() => document.cookie = 'updated=foobar') + await page.evaluate(() => document.cookie = `browser-object-default=${encodeURIComponent('{"foo":"foobar"}')}`) await page.getByText('Refresh cookie').click() - const text = await page.innerText('pre') + text = await page.innerText('pre') expect(text).toContain('foobar') await page.close() }) diff --git a/test/fixtures/basic/pages/cookies.vue b/test/fixtures/basic/pages/cookies.vue index b4d2dfb67a..7a0c2ffb12 100644 --- a/test/fixtures/basic/pages/cookies.vue +++ b/test/fixtures/basic/pages/cookies.vue @@ -5,7 +5,6 @@ useCookie('set').value = 'set' useCookie('set-to-null').value = null useCookie('set-to-null-with-default', () => 'default').value = null -const updated = useCookie('updated') // the next set are all sent by browser useCookie('browser-accessed-but-not-used') useCookie('browser-accessed-with-default-value', () => 'default') @@ -13,7 +12,8 @@ useCookie('browser-set').value = 'set' useCookie('browser-set-to-null').value = null useCookie('browser-set-to-null-with-default', () => 'default').value = null -const objectCookie = useCookie('browser-object-default', { +const objectCookie = useCookie('browser-object-default') +const objectCookieSecond = useCookie('browser-object-default', { default: () => ({ foo: 'bar' }) }) @@ -21,11 +21,11 @@ const objectCookie = useCookie('browser-object-default', {