Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In DEV_MODE, render a warning instead of rendering a template's host in the template. #3199

Merged
merged 2 commits into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/forty-bobcats-sneeze.md
@@ -0,0 +1,7 @@
---
'lit-html': patch
---

In DEV_MODE, render a warning instead of rendering a template's host in the template.

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.
15 changes: 15 additions & 0 deletions packages/lit-html/src/lit-html.ts
Expand Up @@ -1455,6 +1455,21 @@ class ChildPart implements Disconnectable {
} else if ((value as TemplateResult)['_$litType$'] !== undefined) {
this._commitTemplateResult(value as TemplateResult);
} else if ((value as Node).nodeType !== undefined) {
if (DEV_MODE && this.options?.host === value) {
this._commitText(
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
`[probable mistake: rendered a template's host in itself ` +
`(commonly caused by writing \${this} in a template]`
);
console.warn(
`Attempted to render the template host`,
value,
`inside itself. This is almost always a mistake, and in dev mode `,
`we render some warning text. In production however, we'll `,
`render it, which will usually result in an error, and sometimes `,
`in the element disappearing from the DOM.`
);
return;
}
this._commitNode(value as Node);
} else if (isIterable(value)) {
this._commitIterable(value);
Expand Down