pinia icon indicating copy to clipboard operation
pinia copied to clipboard

acceptHMRUpdate skips optional attributes without default values

Open bodograumann opened this issue 3 months ago β€’ 3 comments

Reproduction

https://github.com/bodograumann/pinia-hmr-optional-attributes

Steps to reproduce the bug

  1. git clone https://github.com/bodograumann/pinia-hmr-optional-attributes
  2. cd pinia-hmr-optional-attributes
  3. npm install
  4. npm run dev
  5. xdg-open http://localhost:5173
  6. Press the "Increment" button. Sign says "positive".
  7. touch src/store/counter.ts

Expected behavior

All the state should be preserved.

Actual behavior

counter.nr is preserved as 1, but counter.sign is lost. The page shows -, while it should show positive.

Additional information

The relevant code is here: https://github.com/vuejs/pinia/blob/93b5546cf18bc54bb90de2397219dec7360fa697/packages/pinia/src/hmr.ts#L41-L43

bodograumann avatar Mar 13 '24 18:03 bodograumann

The problem is that the removal of the property sign could be intentional so I'm not sure if this is with changing as it would break other HMR cases.

In order to change this behavior, one could get each property with toRaw(newStore.$state) and check if the property is a ref in order to completely replace it rather than patchObject

posva avatar Mar 14 '24 11:03 posva

Sounds to me like your proposed solution would keep the other use case of removing a property intact, right? I think it should also work for us. Normally I would be happy to contribute a PR, but given that my previous PR is still open and we are thus unable to use the latest pinia version, I am not sure how to proceed here :neutral_face:

bodograumann avatar Mar 15 '24 09:03 bodograumann

I do need to check that PR. Currently we won’t be able to release a new version of pinia soon because of other changes πŸ₯²

posva avatar Mar 15 '24 17:03 posva