prazdevs / pinia-plugin-persistedstate

💾 Configurable persistence and rehydration of Pinia stores.
https://prazdevs.github.io/pinia-plugin-persistedstate/
MIT License
2.05k stars 118 forks source link

Setting store values in succession breaks cookie persistence ?? #259

Closed fosterdouglas closed 9 months ago

fosterdouglas commented 10 months ago

Describe the bug

I've come across some strange behavior and have spent many hours trying to debug. At the end, it seems as though setting multiple values in a store in succession causes persistence to fail with cookies?

In this example, any combination of 2 of these lines works properly, but using 3 or more (of any combination), it results in the values of all of them in the cookie being null. Screenshot 2023-11-14 at 18 28 15

The objects I'm setting are relatively small in kb size.

This is obviously absurd and seems like unlikely behavior, but I've really tried everything to fix it. I'm setting the values from a composable, but I've tried it directly in a component as well.

I'm using 3.2.0, and 2.1.7 of Pinia, with no custom options in nuxt.config, and a basic Setup store:

export const useContextStore = defineStore(
  'context-store',
  () => {
    // STATES //
    const companyContext = ref<Company>(null)
    const userContext = ref<User>(null)
    const productContext = ref<Product>(null)
    const userPhoneContext = ref<UserPhone>(null)

    const authStatusMessage = ref('')
    const appNavigationExpanded = ref(false)

    // ACTIONS //
    function $reset() {
      companyContext.value = null
      userContext.value = null
      productContext.value = null
      userPhoneContext.value = null

      authStatusMessage.value = ''
      appNavigationExpanded.value = false
    }

    return {
      companyContext,
      userContext,
      productContext,
      userPhoneContext,
      authStatusMessage,
      appNavigationExpanded,
      $reset,
    }
  },
  {
    persist: true,
  }
)

If anyone has any ideas of why this might be happening or how I can approach debugging, let me know! Thank you!

Reproduction

-

System Info

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1
    Memory: 1.19 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.7.0 - ~/.nvm/versions/node/v20.7.0/bin/node
    npm: 10.1.0 - ~/.nvm/versions/node/v20.7.0/bin/npm
  Browsers:
    Chrome: 119.0.6045.123
    Safari: 17.0

Used Package Manager

npm

Validations

prazdevs commented 9 months ago

this seems oddly related to #52 at first glance 🤔

MikkelSVDK commented 9 months ago

I encountered a similar issue, and it turned out I was exceeding the cookie size limit of 4096 bytes. Could this be the case for you too? Check the data size stored in the cookie.

fosterdouglas commented 9 months ago

I encountered a similar issue, and it turned out I was exceeding the cookie size limit of 4096 bytes. Could this be the case for you too? Check the data size stored in the cookie.

That was exactly my issue as well! I forgot to revisit this to update.