Skip to content

Commit

Permalink
Fix for #21: Ensure that custom element attribute state is not lost a…
Browse files Browse the repository at this point in the history
…fter HMR updates and that we persist props the same way that normal HMR does when done entirely within Svelte (i.e. restore defaults after HMR only if prop is undefined).
  • Loading branch information
patricknelson committed Nov 17, 2023
1 parent f18b6cf commit d74860e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,19 @@ export default function(opts) {
// already case-insensitive, see: https://dom.spec.whatwg.org/#namednodemap
let attribValue = this.attributes.getNamedItem(propName);
if (attribValue !== null) {
return attribValue.value;
// Before returning, ensure the prop is at least initialized on the target. This ensures that Vite HMR
// will be aware that the prop exists when creating the proxied component (since it enumerates all props).
// This prevents it from resetting back to the props default state during HMR reloads (the same as how it
// works if the component were to have been defined inside of another Svelte component instead of as a
// custom element here).
return target[propName] = attribValue.value;
} else {
// IMPORTANT: Unlike above, we SHOULD NOT be initializing target[propName] here, even though it could offer benefits
// (like allowing the latest *evolved* prop value to be persisted after HMR updates). The reason is that
// Svelte itself will *also* reset the prop to its default value after HMR updates *unless* the parent Svelte
// component explicitly sets the prop. If we set it here, we would diverge from how Svelte handles undefined
// props during HMR reloads.

// Fail over to what would have otherwise been returned.
return target[prop];
}
Expand Down

0 comments on commit d74860e

Please sign in to comment.