Skip to content

Commit

Permalink
fix(nuxt): don't share object between raw cookie and cookie ref (#25255)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniluk4000 authored Jan 17, 2024
1 parent 6c60327 commit 6ca3a6f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function useCookie<T = string | null | undefined> (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))
}

Expand Down Expand Up @@ -107,7 +109,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
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) {
return parse(getRequestHeader(useRequestEvent(), 'cookie') || '', opts)
} else if (import.meta.client) {
Expand Down
2 changes: 2 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/pages/cookies.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const objectCookie = useCookie('browser-object-default', {
<div>
<div>cookies testing page</div>
<pre>{{ objectCookie }}</pre>
<button @click="objectCookie.foo = 'baz'">
<button @click="objectCookie.foo === 'baz' ? objectCookie.foo = 'bar' : objectCookie.foo = 'baz'">
Change cookie
</button>
</div>
Expand Down

0 comments on commit 6ca3a6f

Please sign in to comment.