vuejs / pinia

🍍 Intuitive, type safe, light and flexible Store for Vue using the composition api with DevTools support
https://pinia.vuejs.org
MIT License
13.01k stars 1.04k forks source link

State marked with `skipHydrate` should not be included in the Nuxt payload #2773

Open kabalage opened 2 weeks ago

kabalage commented 2 weeks ago

What problem is this solving

I expected that skipHydrate would also leave the ref out of the Nuxt payload. Nuxt throws errors when it cannot serialize certain objects. I could keep unserializable stuff out of the pinia stores, but I don't like the idea of introducing multiple solutions for shared state. I would much prefer to use Pinia for all shared state.

Example CodeSandbox

Proposed solution

Make skipHydrate also skip transferring of values to the nuxt payload.

Describe alternatives you've considered

No response

posva commented 2 weeks ago

This should be doable with a payload plugin that turns marked properties into into undefined or any other value

posva commented 2 weeks ago

BTW, you can (and should)use that for complex state. I go into all the details in my mastering pinia course

kabalage commented 2 weeks ago

I think I just did that. I created a wrapper called skipSerialize(), in which I used a Symbol to mark the object similarly to how skipHydrate does it, and then I used definePayloadPlugin() to take control of its serialization.

Would it be beneficial to include this (or something similar) in the core library, or would you prefer to keep it separate? I could set up a PR if you're interested.

Edit: Yeah I agree that this is not something you want to do often, but we need it in a particularly complex part of our app.

posva commented 6 days ago

We can reuse the skipHydrate. The data was unused anyway. I pushed to feat/nuxt-skip-hydrate-in-payload if you want to give it a try. For some reason the payload plugin is not being applied.

kabalage commented 6 days ago

I'll give it a try maybe tomorrow. I glanced trough the code quickly, and the problem might be that you need to return a truthy value in the reducer to make it count as a match.

definePayloadReducer(
  'skipHydrate',
  (data: unknown) => !shouldHydrate(data) && 1
)