From 04c86258430b1adf7718c03171e0d832e5bf0f66 Mon Sep 17 00:00:00 2001 From: Mehdi HosseinZade Date: Mon, 19 Dec 2022 15:20:46 +0330 Subject: [PATCH] feat(nuxt): deep watch `useCookie` ref value by default (#9664) * feat(nuxt): useCookie add deep watch option * docs(api): useCookie add deepWatch option * feat(nuxt): useCookie change deepWatch to watch option * boolean|shallow * enable watch by default * docs: fix example * docs(api): update useCookie example Co-authored-by: Pooya Parsa Co-authored-by: Daniel Roe --- .../1.docs/3.api/1.composables/use-cookie.md | 79 ++++++++++++++++--- packages/nuxt/src/app/composables/cookie.ts | 9 ++- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/docs/content/1.docs/3.api/1.composables/use-cookie.md b/docs/content/1.docs/3.api/1.composables/use-cookie.md index 5d3b9dc57d..304857d09b 100644 --- a/docs/content/1.docs/3.api/1.composables/use-cookie.md +++ b/docs/content/1.docs/3.api/1.composables/use-cookie.md @@ -25,16 +25,10 @@ The example below creates a cookie called `counter`. If the cookie doesn't exist ```vue @@ -138,6 +132,71 @@ be returned as the cookie's value. Specifies a function that returns the cookie's default value. The function can also return a `Ref`. +### `watch` + +Specifies the `boolean` or `string` value for [watch](https://vuejs.org/api/reactivity-core.html#watch) cookie ref data. + +- `true` - Will watch cookie ref data changes and its nested properties. (default) +- `shallow` - Will watch cookie ref data changes for only top level properties +- `false` Will not watch cookie ref data changes. + +**Example 1:** + +```vue + + + +``` + +**Example 2:** + +```vue + + + +``` + ## Handling Cookies in API Routes You can use `getCookie` and `setCookie` from [`h3`](https://github.com/unjs/h3) package to set cookies in server API routes. diff --git a/packages/nuxt/src/app/composables/cookie.ts b/packages/nuxt/src/app/composables/cookie.ts index e2e510cf71..10454caec8 100644 --- a/packages/nuxt/src/app/composables/cookie.ts +++ b/packages/nuxt/src/app/composables/cookie.ts @@ -15,12 +15,14 @@ export interface CookieOptions extends _CookieOptions { decode?(value: string): T encode?(value: T): string default?: () => T | Ref + watch?: boolean | 'shallow' } export interface CookieRef extends Ref {} const CookieDefaults: CookieOptions = { path: '/', + watch: true, decode: val => destr(decodeURIComponent(val)), encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val)) } @@ -32,7 +34,12 @@ export function useCookie (name: string, _opts?: CookieOptio const cookie = ref(cookies[name] as any ?? opts.default?.()) if (process.client) { - watch(cookie, () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) }) + const callback = () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) } + if (opts.watch) { + watch(cookie, callback, { deep: opts.watch !== 'shallow' }) + } else { + callback() + } } else if (process.server) { const nuxtApp = useNuxtApp() const writeFinalCookieValue = () => {