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

Conversation

rictic
Copy link
Collaborator

@rictic rictic commented Aug 5, 2022

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.

…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.
@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

🦋 Changeset detected

Latest commit: e215a65

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

This PR includes changesets to release 1 package
Name Type
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

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

📊 Tachometer Benchmark Results

Summary

nop-update

  • lit-html-kitchen-sink: unsure 🔍 -6% - +2% (-1.85ms - +0.58ms)
    this-change vs tip-of-tree

render

  • lit-element-list: 82.67ms - 89.47ms
  • lit-html-kitchen-sink: unsure 🔍 -2% - +2% (-0.56ms - +0.55ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -4% - +1% (-0.44ms - +0.09ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -4% - +1% (-2.14ms - +0.54ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -0% - +4% (-0.18ms - +2.45ms)
    this-change vs tip-of-tree

update

  • lit-element-list: 848.88ms - 875.48ms
  • lit-html-kitchen-sink: unsure 🔍 -4% - +2% (-3.67ms - +1.54ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -2% - +1% (-7.30ms - +3.83ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -2% - +2% (-2.81ms - +3.00ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -3% - +1% (-23.17ms - +12.29ms)
    this-change vs tip-of-tree

update-reflect

  • lit-element-list: 847.63ms - 869.48ms
  • reactive-element-list: unsure 🔍 -1% - +4% (-5.22ms - +43.40ms)
    this-change vs tip-of-tree

Results

lit-element-list

render

VersionAvg timevs
82.67ms - 89.47ms-

update

VersionAvg timevs
848.88ms - 875.48ms-

update-reflect

VersionAvg timevs
847.63ms - 869.48ms-
lit-html-kitchen-sink

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
32.67ms - 33.57ms-unsure 🔍
-2% - +2%
-0.56ms - +0.55ms
unsure 🔍
-2% - +2%
-0.83ms - +0.53ms
tip-of-tree
tip-of-tree
32.80ms - 33.45msunsure 🔍
-2% - +2%
-0.55ms - +0.56ms
-unsure 🔍
-2% - +1%
-0.75ms - +0.46ms
previous-release
previous-release
32.76ms - 33.78msunsure 🔍
-2% - +2%
-0.53ms - +0.83ms
unsure 🔍
-1% - +2%
-0.46ms - +0.75ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
91.15ms - 94.75ms-unsure 🔍
-4% - +2%
-3.67ms - +1.54ms
unsure 🔍
-4% - +2%
-3.50ms - +1.67ms
tip-of-tree
tip-of-tree
92.13ms - 95.89msunsure 🔍
-2% - +4%
-1.54ms - +3.67ms
-unsure 🔍
-3% - +3%
-2.49ms - +2.79ms
previous-release
previous-release
92.01ms - 95.71msunsure 🔍
-2% - +4%
-1.67ms - +3.50ms
unsure 🔍
-3% - +3%
-2.79ms - +2.49ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
29.15ms - 29.97ms-unsure 🔍
-6% - +2%
-1.85ms - +0.58ms
unsure 🔍
-3% - +1%
-0.85ms - +0.44ms
tip-of-tree
tip-of-tree
29.06ms - 31.34msunsure 🔍
-2% - +6%
-0.58ms - +1.85ms
-unsure 🔍
-3% - +6%
-0.82ms - +1.68ms
previous-release
previous-release
29.27ms - 30.27msunsure 🔍
-1% - +3%
-0.44ms - +0.85ms
unsure 🔍
-6% - +3%
-1.68ms - +0.82ms
-
lit-html-repeat

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
11.07ms - 11.45ms-unsure 🔍
-4% - +1%
-0.44ms - +0.09ms
unsure 🔍
-3% - +2%
-0.34ms - +0.19ms
tip-of-tree
tip-of-tree
11.24ms - 11.63msunsure 🔍
-1% - +4%
-0.09ms - +0.44ms
-unsure 🔍
-2% - +3%
-0.17ms - +0.38ms
previous-release
previous-release
11.14ms - 11.53msunsure 🔍
-2% - +3%
-0.19ms - +0.34ms
unsure 🔍
-3% - +1%
-0.38ms - +0.17ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
324.30ms - 331.08ms-unsure 🔍
-2% - +1%
-7.30ms - +3.83ms
unsure 🔍
-2% - +1%
-5.19ms - +3.86ms
tip-of-tree
tip-of-tree
325.01ms - 333.83msunsure 🔍
-1% - +2%
-3.83ms - +7.30ms
-unsure 🔍
-1% - +2%
-4.26ms - +6.40ms
previous-release
previous-release
325.36ms - 331.34msunsure 🔍
-1% - +2%
-3.86ms - +5.19ms
unsure 🔍
-2% - +1%
-6.40ms - +4.26ms
-
lit-html-template-heavy

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
58.58ms - 59.99ms-unsure 🔍
-4% - +1%
-2.14ms - +0.54ms
unsure 🔍
-3% - +1%
-1.61ms - +0.40ms
tip-of-tree
tip-of-tree
58.95ms - 61.23msunsure 🔍
-1% - +4%
-0.54ms - +2.14ms
-unsure 🔍
-2% - +3%
-1.15ms - +1.55ms
previous-release
previous-release
59.18ms - 60.61msunsure 🔍
-1% - +3%
-0.40ms - +1.61ms
unsure 🔍
-3% - +2%
-1.55ms - +1.15ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
126.51ms - 130.63ms-unsure 🔍
-2% - +2%
-2.81ms - +3.00ms
unsure 🔍
-2% - +3%
-2.57ms - +3.29ms
tip-of-tree
tip-of-tree
126.43ms - 130.53msunsure 🔍
-2% - +2%
-3.00ms - +2.81ms
-unsure 🔍
-2% - +2%
-2.66ms - +3.19ms
previous-release
previous-release
126.13ms - 130.30msunsure 🔍
-3% - +2%
-3.29ms - +2.57ms
unsure 🔍
-2% - +2%
-3.19ms - +2.66ms
-
reactive-element-list

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
61.72ms - 63.46ms-unsure 🔍
-0% - +4%
-0.18ms - +2.45ms
unsure 🔍
-1% - +3%
-0.49ms - +2.02ms
tip-of-tree
tip-of-tree
60.47ms - 62.44msunsure 🔍
-4% - +0%
-2.45ms - +0.18ms
-unsure 🔍
-3% - +2%
-1.71ms - +0.97ms
previous-release
previous-release
60.92ms - 62.73msunsure 🔍
-3% - +1%
-2.02ms - +0.49ms
unsure 🔍
-2% - +3%
-0.97ms - +1.71ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
868.58ms - 891.88ms-unsure 🔍
-3% - +1%
-23.17ms - +12.29ms
unsure 🔍
-2% - +2%
-18.80ms - +17.14ms
tip-of-tree
tip-of-tree
872.30ms - 899.04msunsure 🔍
-1% - +3%
-12.29ms - +23.17ms
-unsure 🔍
-2% - +3%
-14.52ms - +23.73ms
previous-release
previous-release
867.39ms - 894.74msunsure 🔍
-2% - +2%
-17.14ms - +18.80ms
unsure 🔍
-3% - +2%
-23.73ms - +14.52ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
982.13ms - 1015.24ms-unsure 🔍
-1% - +4%
-5.22ms - +43.40ms
unsure 🔍
-0% - +5%
-0.28ms - +51.68ms
tip-of-tree
tip-of-tree
961.79ms - 997.40msunsure 🔍
-4% - +1%
-43.40ms - +5.22ms
-unsure 🔍
-2% - +3%
-20.18ms - +33.40ms
previous-release
previous-release
952.97ms - 993.00msunsure 🔍
-5% - +0%
-51.68ms - +0.28ms
unsure 🔍
-3% - +2%
-33.40ms - +20.18ms
-

tachometer-reporter-action v2 for Benchmarks

@justinfagnani
Copy link
Collaborator

Whoa, this is an unexpected feature to need, but with HMR I get it!

But doesn't this potentially apply to any element? What if you wanted to render the id of a child, like ${someEl.id}?

Maybe the solution should be to not render any element that already has a parent?

I'm also a little surprised that this doesn't just cause an error, since you shouldn't be able to add an element to a descendent as that would cause a cycle in the DOM.

@rictic
Copy link
Collaborator Author

rictic commented Aug 5, 2022

I think it's legit and not that uncommon to reparent a node in general with Lit, but it's almost definitely a mistake to render the host assuming that the host contains the template, as is typical.

@justinfagnani
Copy link
Collaborator

I think it's legit and not that uncommon to reparent a node in general with Lit

I don't think I've ever seen that. I've definitely seen new, non-parented node be inserted with Lit, but not nodes with a parent already.

The main case I could imagine is if the parent is a DocumentFragment like from cloning a template, but since we have the templateContent directive, I haven't seen that either.

@rictic
Copy link
Collaborator Author

rictic commented Aug 5, 2022

One use case:

render() {
  if (this.noChrome) {
    return html`${this.contentEl}`;
  }
  return html`
    <header>...</header>
    <main>${this.contentEl}</main>
    <footer>...</footer>
  `;
}

Yeah it's just moving the element around inside the same shadow root, but that's legit! Or for a slightly harder to detect example, you could imagine passing contentEl to one of two kinds of rendering element.

It's somewhat weird, but there are cases where this is a pretty good design, and if we block it here we're effectively banning it.

@justinfagnani
Copy link
Collaborator

otoh, a pattern I have seen not-uncommonly (especially in server-rendered content) is light-DOM-as-data:

render() {
  const title = this.querySelector('x-title');
  return html`<h1>${title.textContent}`</h1>`;
}

So how do we keep from destructively updating the DOM when the expression is just ${title} while typing?

I'm wondering if we need an HMR mode with options for these things...

@rictic rictic enabled auto-merge (squash) August 5, 2022 20:08
@rictic rictic merged commit 0725fdb into main Aug 5, 2022
@rictic rictic deleted the no-render-this branch August 5, 2022 20:15
@lit-robot lit-robot mentioned this pull request Aug 11, 2022
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.

None yet

2 participants