Skip to content

fix(lit-html): handle undefined arg on disconnect for ref directive - #5218

Merged
justinfagnani merged 3 commits into
lit:mainfrom
louis-bompart:fix/5217
Mar 12, 2026
Merged

justinfagnani merged 3 commits into
lit:mainfrom
louis-bompart:fix/5217

Conversation

@louis-bompart

Copy link
Copy Markdown
Contributor

When ref is called with undefined for arg and the element where the ref has been render in disconnect, the disconnect method tried to call this._ref.value, where this._ref being undefined, causing an error.

The _updateRefValue method in essence currently already sieve thru the various type of the _ref member. This PR handles the undefined case by returning early.

Fixes #5217

@changeset-bot

changeset-bot Bot commented Jan 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c972037

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
lit Patch
lit-html Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

override update(part: ElementPart, [ref]: Parameters<this['render']>) {
const refChanged = ref !== this._ref;
if (refChanged && this._ref !== undefined) {
if (refChanged) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an over-optimization, but we would have been checking twice whether this._ref is undefined in this code path.
Given that _updateRefValue handles it after my change, I removed this part of the condition.

@sorvell
sorvell requested a review from justinfagnani February 4, 2026 01:47
Comment thread .changeset/sweet-garlics-behave.md Resolved
@justinfagnani
justinfagnani merged commit be6b275 into lit:main Mar 12, 2026
8 checks passed
kyubisation pushed a commit to kyubisation/lit that referenced this pull request May 7, 2026
@lit-robot lit-robot mentioned this pull request May 14, 2026
max-sixty added a commit to max-sixty/leaf that referenced this pull request Oct 11, 2026
Package and runtime templates can import the Lit helpers they need
through the shared facade: nothing, live, ref/createRef, classMap,
styleMap, guard, svg/mathml, when, ifDefined, keyed and unsafeHTML. The
widget API keeps Leaf’s focus-preserving render wrapper, and the runtime
no longer imports Lit directly. This re-exports the existing vendored
singleton and keeps the dependency pin. The Lit build backports
[upstream #5218](lit/lit#5218) so optional
ref(undefined) survives disconnect/reconnect; lit-html stays at 3.3.0 to
avoid the repeat marker leak. The authoring reference explains when live
restores an application-owned native value after a refused gesture.

Survey item 2. Measured source lines: browser/index.ts 12 → 23;
widget-api.js 250 → 267; widget-elements.js 595 → 595. These are API
additions, not a line reduction; generated facade and build assertions
are also updated. The ref backport grows browser/build.mjs from 306 to
326 lines.

Validation: focused browser-build tests (7 passed), runtime tests (313
passed), served base/candidate startup comparison, and optional-ref
lifecycle regressions in Node and Chromium.

> _This was written by Codex on behalf of max-sixty_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ref directive does not san-check its stored arg on disconnected/reconnected lifecycle

4 participants