CustomElementRegistry: upgrade() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

The upgrade() method of the CustomElementRegistry interface upgrades all shadow-containing custom elements in a Node subtree, even before they are connected to the main document.

Syntax

js
upgrade(root)

Parameters

root

A Node instance with shadow-containing descendant elements to upgrade. If there are no descendant elements that can be upgraded, no error is thrown.

Return value

None (undefined).

Description

When an HTML element is parsed or created, it may use a tag name that corresponds to a custom element (e.g., <my-element>). If the custom element's class has not yet been registered with the appropriate CustomElementRegistry at the time the element is created, the element exists as an undefined, plain HTMLElement. It looks and behaves like any unknown element — it has no special behavior, lifecycle callbacks, or custom prototype methods.

Upgrading is the process of retroactively promoting such an element to a full-fledged custom element once its definition becomes available. When an element is upgraded:

  1. Its prototype is swapped to the custom element class that was registered with define().
  2. Its connectedCallback() and any other applicable lifecycle callbacks are invoked.
  3. If the class defines observedAttributes, the attributeChangedCallback() is called for each attribute that already has a value.

Normally, elements are upgraded automatically when their definition is registered via define(), but only if they are already connected to the document. The upgrade() method is useful when you need to upgrade elements that exist in a disconnected DOM subtree (for example, elements created via Document.createElement() or parsed into a DocumentFragment) before they are inserted into the document.

Examples

Taken from the HTML spec:

js
const el = document.createElement("spider-man");

class SpiderMan extends HTMLElement {}
customElements.define("spider-man", SpiderMan);

console.assert(!(el instanceof SpiderMan)); // not yet upgraded

customElements.upgrade(el);
console.assert(el instanceof SpiderMan); // upgraded!

Specifications

Specification
HTML
# dom-customelementregistry-upgrade-dev

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
upgrade
Chrome – Full support
Chrome 68 (Release date: 2018-07-24)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 63 (Release date: 2018-10-23)
footnote Full support
Opera – Full support
Opera 55 (Release date: 2018-08-16)
footnote Full support
Safari – Full support
Safari 12.1 (Release date: 2019-03-25)
footnote Full support
Chrome Android – Full support
Chrome Android 68 (Release date: 2018-07-24)
footnote Full support
Firefox for Android – Full support
Firefox for Android 63 (Release date: 2018-10-23)
footnote Full support
Opera Android – Full support
Opera Android 48 (Release date: 2018-11-08)
footnote Full support
Safari on iOS – Full support
Safari on iOS 12.2 (Release date: 2019-03-25)
footnote Full support
Samsung Internet – Full support
Samsung Internet 10 (Release date: 2019-08-22)
footnote Full support
WebView Android – Full support
WebView Android 68 (Release date: 2018-07-24)
footnote Full support
WebView on iOS – Full support
WebView on iOS 12.2 (Release date: 2019-03-25)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support