Skip to content

Commit

Permalink
fix(nuxt): use addEventListener to register cookie store listener (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 18, 2024
1 parent a6af09e commit 2417848
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/nuxt/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ declare global {

interface Window {
cookieStore?: {
onchange: (event: any) => void
addEventListener: (type: 'change', listener: (event: any) => void) => void
removeEventListener: (type: 'change', listener: (event: any) => void) => void
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
const handleChange = (data: { value?: any, refresh?: boolean }) => {
const value = data.refresh ? readRawCookies(opts)?.[name] : opts.decode(data.value)
watchPaused = true
cookies[name] = cookie.value = value
cookie.value = value
cookies[name] = klona(value)
nextTick(() => { watchPaused = false })
}

let watchPaused = false

if (getCurrentScope()) {
const hasScope = !!getCurrentScope()

if (hasScope) {
onScopeDispose(() => {
watchPaused = true
callback()
Expand All @@ -98,10 +101,14 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
}

if (store) {
store.onchange = (event) => {
const changeHandler = (event: any) => {
const cookie = event.changed.find((c: any) => c.name === name)
if (cookie) { handleChange({ value: cookie.value }) }
}
store.addEventListener('change', changeHandler)
if (hasScope) {
onScopeDispose(() => store.removeEventListener('change', changeHandler))
}
} else if (channel) {
channel.onmessage = ({ data }) => handleChange(data)
}
Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ describe('nuxt composables', () => {
},
})
const cookies = res.headers.get('set-cookie')
expect(cookies).toMatchInlineSnapshot('"set-in-plugin=true; Path=/, set=set; Path=/, browser-set=set; Path=/, browser-set-to-null=; Max-Age=0; Path=/, browser-set-to-null-with-default=; Max-Age=0; Path=/, browser-object-default=%7B%22foo%22%3A%22bar%22%7D; Path=/"')
expect(cookies).toMatchInlineSnapshot('"set-in-plugin=true; Path=/, accessed-with-default-value=default; Path=/, set=set; Path=/, browser-set=set; Path=/, browser-set-to-null=; Max-Age=0; Path=/, browser-set-to-null-with-default=; Max-Age=0; Path=/, browser-object-default=%7B%22foo%22%3A%22bar%22%7D; Path=/"')
})
it('updates cookies when they are changed', async () => {
const { page } = await renderPage('/cookies')
Expand Down
23 changes: 16 additions & 7 deletions test/fixtures/basic/pages/cookies.vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
<script setup>
<script setup lang="ts">
useCookie('accessed-but-not-used')
useCookie('accessed-with-default-value', () => 'default')
useCookie('accessed-with-default-value', { default: () => 'default' })
useCookie('set').value = 'set'
useCookie('set-to-null').value = null
useCookie('set-to-null-with-default', () => 'default').value = null
useCookie<string | null>('set-to-null-with-default', { default: () => 'default' }).value = null
// the next set are all sent by browser
useCookie('browser-accessed-but-not-used')
useCookie('browser-accessed-with-default-value', () => 'default')
useCookie('browser-accessed-with-default-value', { default: () => 'default' })
useCookie('browser-set').value = 'set'
useCookie('browser-set-to-null').value = null
useCookie('browser-set-to-null-with-default', () => 'default').value = null
useCookie<string | null>('browser-set-to-null-with-default', { default: () => 'default' }).value = null
const objectCookie = useCookie('browser-object-default')
const objectCookie = useCookie<{ foo: string } | undefined>('browser-object-default')
const objectCookieSecond = useCookie('browser-object-default', {
default: () => ({ foo: 'bar' }),
})
function changeCookie () {
console.log(objectCookie.value, objectCookieSecond.value)
if (objectCookie.value!.foo === 'baz') {
objectCookie.value!.foo = 'bar'
} else {
objectCookie.value!.foo = 'baz'
}
}
</script>

<template>
<div>
<div>cookies testing page</div>
<pre>{{ objectCookie?.foo }}</pre>
<pre>{{ objectCookieSecond.foo }}</pre>
<button @click="objectCookie.foo === 'baz' ? objectCookie.foo = 'bar' : objectCookie.foo = 'baz'">
<button @click="changeCookie">
Change cookie
</button>
<button @click="refreshCookie('browser-object-default')">
Expand Down

0 comments on commit 2417848

Please sign in to comment.