Repository navigation
fix(hmr): preserve runtime-added properties of setup store refs - #3132
Conversation
In a setup store, state created with `ref({ ... })` can gain new
properties at runtime (e.g. an action setting an optional field). During
HMR the new module reinitializes the ref to its declared value, and
`patchObject` reconciled the old state against that fresh value, dropping
any key the new value did not declare.
This is correct for option stores, whose whole state shape is declared
upfront in `state()`, but wrong for setup stores where the shape is
imperative. Only run `patchObject` for option stores; for setup stores
transfer the old ref value as a whole, matching the behavior of the
function form of `$patch` and direct assignment.
Closes vuejs#2611
📝 WalkthroughWalkthroughThis PR fixes a Hot Module Replacement (HMR) bug in Pinia where setup stores lose runtime-added properties during module reloads. The implementation adds conditional state reconciliation logic, and new tests validate preservation of both runtime-added properties and nested object values. ChangesSetup Store HMR State Preservation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Closes #2611
Problem
In a setup store, state created with
ref({ ... })can gain new properties at runtime — e.g. an action that sets an optional field:After clicking increment,
counteris{ nr: 1, sign: 'positive' }. On HMR, the new module reinitializescounterto its declared value{ nr: 0 }(nosign).acceptHMRUpdatethen calledpatchObject(newValue, oldValue), which removes any key not present in the new value — so the runtime-addedsignwas dropped, even thoughnrwas preserved.Fix
patchObject's removal semantics are correct for option stores, whose entire state shape is declared upfront instate(): a key missing from the new definition was intentionally removed. But setup stores build their state imperatively (ref({}),reactive({})), and properties may be added at runtime that were never part of any declaration — diffing them against the freshly-initialized value wrongly removes them.The one-line change gates
patchObjectbehindisOptionsStore. For setup stores the old value is transferred as a whole (theelsebranch already does exactly this — "transfer the ref" — for primitive/non-plain-object refs), which is also consistent with how the function form of$patchand direct assignment behave.Tests
it.todo('handles nested objects updates')in the setup-store block is replaced with two real tests:keeps runtime-added properties of a ref object (#2611)— the exact reported scenario.handles nested objects updates— a runtime-mutated nested value survives HMR even when the new definition drops a key.Both fail before the change and pass after. The existing option-store tests (
patches nested objects,skips arrays,skips nested arrays) are unchanged and still pass — removal behavior for option stores is preserved. Full suite: 204 passing.Summary by CodeRabbit