fix(nuxt): don't share object between raw cookie and cookie ref (#25255)

This commit is contained in:
Danila Rodichkin 2024-01-17 14:53:14 +03:00 committed by GitHub
parent 6c6032782f
commit 6ca3a6fecb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 2 deletions

View File

@ -61,6 +61,8 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
const callback = () => { const callback = () => {
if (opts.readonly || isEqual(cookie.value, cookies[name])) { return } if (opts.readonly || isEqual(cookie.value, cookies[name])) { return }
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)
cookies[name] = klona(cookie.value)
channel?.postMessage(opts.encode(cookie.value as T)) channel?.postMessage(opts.encode(cookie.value as T))
} }
@ -107,7 +109,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
return cookie as CookieRef<T> return cookie as CookieRef<T>
} }
function readRawCookies (opts: CookieOptions = {}): Record<string, string> | undefined { function readRawCookies (opts: CookieOptions = {}): Record<string, unknown> | undefined {
if (import.meta.server) { if (import.meta.server) {
return parse(getRequestHeader(useRequestEvent(), 'cookie') || '', opts) return parse(getRequestHeader(useRequestEvent(), 'cookie') || '', opts)
} else if (import.meta.client) { } else if (import.meta.client) {

View File

@ -501,6 +501,8 @@ describe('nuxt composables', () => {
expect(await extractCookie()).toEqual({ foo: 'bar' }) expect(await extractCookie()).toEqual({ foo: 'bar' })
await page.getByRole('button').click() await page.getByRole('button').click()
expect(await extractCookie()).toEqual({ foo: 'baz' }) expect(await extractCookie()).toEqual({ foo: 'baz' })
await page.getByRole('button').click()
expect(await extractCookie()).toEqual({ foo: 'bar' })
await page.close() await page.close()
}) })
}) })

View File

@ -20,7 +20,7 @@ const objectCookie = useCookie('browser-object-default', {
<div> <div>
<div>cookies testing page</div> <div>cookies testing page</div>
<pre>{{ objectCookie }}</pre> <pre>{{ objectCookie }}</pre>
<button @click="objectCookie.foo = 'baz'"> <button @click="objectCookie.foo === 'baz' ? objectCookie.foo = 'bar' : objectCookie.foo = 'baz'">
Change cookie Change cookie
</button> </button>
</div> </div>