Skip to content

Commit 440d058

Browse files
authored
Merge e215a65 into 6361a4b
2 parents 6361a4b + e215a65 commit 440d058

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

.changeset/forty-bobcats-sneeze.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'lit-html': patch
3+
---
4+
5+
In DEV_MODE, render a warning instead of rendering a template's host in the template.
6+
7+
Most commonly this would happen when rendering `${this}` in a LitElement's template, which has the counterintuitive behavior of removing the element from the DOM, because when rendering the element's template we attach it into its own shadow root, which removes it from the DOM, causing it simply disappear. This is especially problematic with a fast HMR system.

packages/lit-html/src/lit-html.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,21 @@ class ChildPart implements Disconnectable {
14551455
} else if ((value as TemplateResult)['_$litType$'] !== undefined) {
14561456
this._commitTemplateResult(value as TemplateResult);
14571457
} else if ((value as Node).nodeType !== undefined) {
1458+
if (DEV_MODE && this.options?.host === value) {
1459+
this._commitText(
1460+
`[probable mistake: rendered a template's host in itself ` +
1461+
`(commonly caused by writing \${this} in a template]`
1462+
);
1463+
console.warn(
1464+
`Attempted to render the template host`,
1465+
value,
1466+
`inside itself. This is almost always a mistake, and in dev mode `,
1467+
`we render some warning text. In production however, we'll `,
1468+
`render it, which will usually result in an error, and sometimes `,
1469+
`in the element disappearing from the DOM.`
1470+
);
1471+
return;
1472+
}
14581473
this._commitNode(value as Node);
14591474
} else if (isIterable(value)) {
14601475
this._commitIterable(value);

0 commit comments

Comments
 (0)