Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useCookie doesn't write new value in cookies storage if composable used in multiple components #987

Closed
evgenii-code opened this issue Nov 24, 2023 · 2 comments

Comments

@evgenii-code
Copy link

Environment

  • Operating System: Windows_NT
  • Node Version: v18.18.2
  • Nuxt Version: 2.17.2
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.0
  • Package Manager: npm@9.8.1
  • Builder: webpack
  • User Config: bridge, head, css, plugins, components, modules, build, serverHandlers, devServerHandlers, devServer, typescript, nitro, buildModules
  • Runtime Modules: -
  • Build Modules: (), @nuxt/bridge@3.0.0-rc.3-28343365.3c40b3f

Reproduction

https://github.com/evgenii-code/nuxt-bridge-use-cookies

// install dependencies
npm i

// run dev server
npm run dev

Describe the bug

We have page with one component that uses multiple times.
Each component uses useCookies composable to read and write values to cookie storage with the same cookie's name.

useCookie doesn't write new value in cookies storage if composable used in multiple components.

First usage of component:

<script setup>
const foo = useCookie('foo')

const setCookie1 = () => (foo.value = '1');
</script>

Second usage of component

<script setup>
const foo = useCookie('foo')

const setCookie2 = () => (foo.value = '2');
</script>

If we run methods in that order: setCookie1, setCookie2 and setCookie1 again, then cookie value in browser's storage stays '2', but should be '1'.

Here is a little video to show of this behaviour
https://imgur.com/gUjDhcc

Code of the component
<template>
  <div>
    <h2>Component {{ id }}</h2>

    <button @click="setCookie">Set cookie to {{ id }}</button>

    <p>{{ `${foo}` }}</p>
  </div>
</template>

<script setup lang="ts">
import { toRefs } from 'vue'
import { useCookie } from '#imports'

const props = defineProps({
  id: {
    type: String,
    required: true,
  },
})

const { id } = toRefs(props)

const foo = useCookie<string | undefined>('foo')

const setCookie = () => (foo.value = id.value)
</script>
Code of the page
<template>
  <div>
    <h1>Bug with useCookie</h1>

    <base-foo id="1" key="1" />

    <base-foo id="2" key="2" />
  </div>
</template>

<script lang="ts">
import { defineNuxtComponent } from '#imports'
import BaseFoo from '~/components/BaseFoo.vue'

export default defineNuxtComponent({
  name: 'PageCookies',

  components: {
    BaseFoo,
  },
})
</script>

Additional context

No response

Logs

No response

@evgenii-code
Copy link
Author

I have figured that when I use useCookie multiple times it creates local reactive state every time.
And when I click setCookie1 after setCookie2 then basically watch inside useCookie doesn't trigger, because the value of foo remains the same in that local reactive state.

This behavior was not clear from the docs. Perhaps we should add this info.

Another idea is to add listeners for browser's cookies events that will trigger reactive updates of the useCookie. That would be a nice feature.

@wattanx
Copy link
Collaborator

wattanx commented Nov 30, 2023

@wattanx wattanx closed this as completed Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants