Skip to content

Conversation

@frehner
Copy link
Contributor

@frehner frehner commented Oct 8, 2025

Description

Updated examples to use document.importNode() exclusively, and references to refer to both importNode and Node.cloneNode.

Motivation

document.importNode and Node.cloneNode are largely similar, but have one important difference; the timing of when the node is adopted. When using Node.cloneNode(), the content isn't adopted until it's either explicitly adopted or when it's appended to the document, so at a later point after calling cloneNode(). This falls into an edge-case annoyance where calling upgrade on nodes that aren't yet adopted causes them to not actually get upgraded. This real example from the Web Components discord demonstrates the edge-case:

class MyEl extends HTMLElement {
  constructor() {super(); console.log('MyEl constructor')}
}
customElements.define('my-el', MyEl)

const template = document.createElement('template')
template.innerHTML = '<my-el></my-el>'

const frag = template.content.cloneNode(true)

console.log(customElements.get('my-el')) // MyEl

customElements.upgrade(frag) // crickets
console.log('My el constructor should have ran already')

document.body.append(frag) // logs "MyEl constructor"

If you change from content.cloneNode() to document.importNode() then the constructor will run and the content is upgraded correctly.

So we would like to update the examples to show document.importNode() being used by default, and also update references/links to show it as an option next to cloneNode.

Additional details

Web components discord discussion https://discord.com/channels/767813449048260658/767813449048260661/1421037073829199994

Related issues and pull requests

@frehner frehner requested review from a team as code owners October 8, 2025 18:55
@frehner frehner requested review from estelle and wbamberg and removed request for a team October 8, 2025 18:55
@github-actions github-actions bot added Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Oct 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2025

Preview URLs

Flaws (3)

Note! 4 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/Web_components/Using_templates_and_slots
Title: Using templates and slots
Flaw count: 3

  • macros:
    • Macro produces link /en-US/docs/Web/Web_Components/Using_custom_elements which is a redirect
    • Macro produces link /en-US/docs/Web/Web_Components/Using_shadow_DOM which is a redirect
    • Macro produces link /en-US/docs/Web/Web_Components/Using_templates_and_slots which is a redirect

(comment last updated: 2025-10-20 15:33:46)

@estelle estelle removed their request for review October 14, 2025 09:32
@Josh-Cena
Copy link
Member

If this is important, could you add a sentence to cloneNode (and importNode)?

@frehner
Copy link
Contributor Author

frehner commented Oct 20, 2025

If this is important, could you add a sentence to cloneNode (and importNode)?

How does that look?

@Josh-Cena
Copy link
Member

If your argument for using importNode is for upgrade, why not point that out directly? There are significant use cases of <template> that aren't related to custom elements at all, and even some that do, do not suffer from this issue if the <template> is just used within the custom element constructor to populate the content—as the majority of our examples do. So it's not totally clear to me how big the impact is. I searched for .cloneNode(true) and we have a lot more usages with template.content; should all of them be replaced as well?

@frehner
Copy link
Contributor Author

frehner commented Oct 21, 2025

If your argument for using importNode is for upgrade, why not point that out directly? There are significant use cases of <template> that aren't related to custom elements at all, and even some that do, do not suffer from this issue if the <template> is just used within the custom element constructor to populate the content—as the majority of our examples do. So it's not totally clear to me how big the impact is. I searched for .cloneNode(true) and we have a lot more usages with template.content; should all of them be replaced as well?

I’m under the impression from the community that yes, we would like all these updated and that importNode should be the default.

I’ll see if I can get some people to chime in here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:HTML Hypertext Markup Language docs Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants