fix(nuxt): broadcast cookie change in correct format (#25598)

This commit is contained in:
Enkot 2024-02-04 01:15:26 +02:00 committed by GitHub
parent c41acfb7ca
commit 876d910c3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 7 deletions

View File

@ -69,7 +69,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) writeClientCookie(name, cookie.value, opts as CookieSerializeOptions)
cookies[name] = klona(cookie.value) 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 }) => { const handleChange = (data: { value?: any, refresh?: boolean }) => {

View File

@ -490,11 +490,13 @@ describe('nuxt composables', () => {
expect(await extractCookie()).toEqual({ foo: 'bar' }) expect(await extractCookie()).toEqual({ foo: 'bar' })
await page.getByText('Change cookie').click() await page.getByText('Change cookie').click()
expect(await extractCookie()).toEqual({ foo: 'baz' }) expect(await extractCookie()).toEqual({ foo: 'baz' })
let text = await page.innerText('pre')
expect(text).toContain('baz')
await page.getByText('Change cookie').click() await page.getByText('Change cookie').click()
expect(await extractCookie()).toEqual({ foo: 'bar' }) 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() await page.getByText('Refresh cookie').click()
const text = await page.innerText('pre') text = await page.innerText('pre')
expect(text).toContain('foobar') expect(text).toContain('foobar')
await page.close() await page.close()
}) })

View File

@ -5,7 +5,6 @@ useCookie('set').value = 'set'
useCookie('set-to-null').value = null useCookie('set-to-null').value = null
useCookie('set-to-null-with-default', () => 'default').value = null useCookie('set-to-null-with-default', () => 'default').value = null
const updated = useCookie('updated')
// the next set are all sent by browser // the next set are all sent by browser
useCookie('browser-accessed-but-not-used') useCookie('browser-accessed-but-not-used')
useCookie('browser-accessed-with-default-value', () => 'default') 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').value = null
useCookie('browser-set-to-null-with-default', () => 'default').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' }) default: () => ({ foo: 'bar' })
}) })
</script> </script>
@ -21,11 +21,11 @@ const objectCookie = useCookie('browser-object-default', {
<template> <template>
<div> <div>
<div>cookies testing page</div> <div>cookies testing page</div>
<pre>{{ updated }}</pre> <pre>{{ objectCookieSecond.foo }}</pre>
<button @click="objectCookie.foo === 'baz' ? objectCookie.foo = 'bar' : objectCookie.foo = 'baz'"> <button @click="objectCookie.foo === 'baz' ? objectCookie.foo = 'bar' : objectCookie.foo = 'baz'">
Change cookie Change cookie
</button> </button>
<button @click="refreshCookie('updated')"> <button @click="refreshCookie('browser-object-default')">
Refresh cookie Refresh cookie
</button> </button>
</div> </div>