Scoped custom element registries
Scoped custom element registries let two versions of a web component share one page. Learn new CustomElementRegistry(), attachShadow(), and initialize().
- 01Explain a name clashSay why a second
define()throws, which copy wins, and what prefixes and renamed tags cost. - 02Give a shadow root its own registryMake one with
new CustomElementRegistry(), pass it toattachShadow()orcreateElement(), and readcustomElementRegistry. - 03Fill a root later, and check supportUse
shadowrootcustomelementregistrywithinitialize(), know it only fills an empty slot, and know what the polyfill can't do.
What are scoped custom element registries?
Leaf and Kettle's checkout has a new guest. Cup Club, a partner's loyalty program, sends a small widget. It shows your free cups, with a Redeem button. Asha puts it next to the shop's Add to cart button. The page loads. The Cup Club box is empty, and the console shows a red error.
Both buttons are versions of one component. The shop uses version 2 of <tea-button>. Cup Club still brings its own copy of version 1. Can two versions of one component share a page?
Yes, with a scoped custom element registry. A scoped registry is a list of tag names and their classes that only part of a page uses, usually one shadow root. Inside that root, <tea-button> is built from the registry's class instead of the page's. So version 1 and version 2 can sit side by side.
Here is the checkout, cut down to the two buttons:
<tea-button label="Add to cart"></tea-button><cup-club></cup-club> <script type="module"> await import("./tea-button.js"); try { await import("./cup-club/cup-club.js"); } catch (error) { console.log(`${error.name}: ${error.message}`); }</script>- Line 1 is the shop's button. Line 2 is Cup Club's widget.
- Line 5 loads the shop's file. In the
tea-button.jstab, line 8 registerstea-buttonwith the version 2 class. - Line 8 loads Cup Club's widget. Its first line imports Cup Club's own copy of the button, version 1. That copy calls
define()fortea-buttontoo. import()loads a file while the page runs, and gives back a promise. Dynamic import, on the JavaScript path, covers it. Lines 7 to 11 catch the error and print it.
The console prints NotSupportedError: Failed to execute 'define' on 'CustomElementRegistry': the name "tea-button" has already been used with this registry. Cup Club's file stopped at that line. So cup-club was never defined, and its box stays empty.
You met this error in lesson 4.3, with two copies of the same file. This time the copies are two different versions, and each one needs the name.
define() throws a NotSupportedError, and its version 1 class is never used.Where do the names live? In the page's registry: the browser's list of tag names and the class for each. Lesson 3.1 showed that customElements.define() writes into it, and that each name fits only once.
A school has one cloakroom, with one hook for each name. Asha from class 3 hangs her coat on the hook marked Asha. Then a new Asha joins class 5. The hook is taken, so her coat has nowhere to go. A cloakroom in each classroom would fix it. Each class would keep its own hooks.
- In real life: The school cloakroom
- In JavaScript: The page's registry,
customElements - In real life: A hook with a name label
- In JavaScript: A tag name and its class
- In real life: The second girl called Asha
- In JavaScript: Cup Club's second
define()fortea-button - In real life: A cloakroom in each classroom
- In JavaScript: A scoped registry for one shadow root
The page's registry holds each tag name once, so two versions of one component clash. A scoped registry is a second list that only one shadow root uses. Make it with new CustomElementRegistry(). Give it to the root with attachShadow({ mode, customElementRegistry }).
Lesson 1.2 showed one scoped registry at work. This lesson takes them apart: the clash, the new registry, the shadow root, Cup Club fixed, and roots that get their registry later.
What happens when two libraries define the same tag name?
The second define() throws, and the first one wins. Every <tea-button> the page's registry builds uses the first class. MDN's guide puts it plainly: if two libraries both try to define <my-button>, "one of them will fail". Try both orders:
<tea-button label="Add to cart"></tea-button><cup-club></cup-club> <script type="module"> await import("./tea-button.js"); try { await import("./cup-club/cup-club.js"); } catch (error) { console.log(`${error.name}: ${error.message}`); }</script>Line 5 loads the shop's file, so tea-button means version 2. On line 8, Cup Club's copy calls define() again and throws. The widget's file stops there, so cup-club is never defined, and its box stays empty.
Pick The shop's file first. The result shows Add to cart (v2), and the Cup Club box stays empty. Now pick Cup Club's file first. The widget works and shows Redeem (v1). But the shop's own button now says Add to cart (v1). The console prints the same error both times.
Why it matters: whichever file runs second breaks. The error names a tag, not the file that caused the trouble.
How does this happen in real apps? The WICG explainer for scoped registries lists the usual ways:
- npm can install two versions of one library when their version ranges don't match.
- Big apps have parts built by different teams, and shipped at different times.
- Plug-ins and browser extensions add their own elements to a page.
- Third-party widgets, like Cup Club's, bring components the page never chose.
Before scoped registries, teams worked around the clash. Read each row across: the way, what it does, and what it costs.
| Way around it | What it does | What it costs |
|---|---|---|
A prefix, like tea- | Each library starts its names with its own word. | Two versions of one library share the prefix, so they still clash. |
A new name for each version, like v2-tea-button | Stencil's tag transformation renames a library's tags when they're defined. | Every CSS selector and querySelector() string that names the tag must change too. |
Check get() before define() | The second copy skips its define() instead of throwing. | The first class wins quietly. Nothing checks that the two versions behave the same. |
| An iframe | The widget runs in a page of its own, with its own registry. | It's a whole separate document, with its own styles and scripts, for one widget. |
Lesson 3.1's prefixes help when two different libraries meet. They don't help two versions of the same library, because both versions use the same prefix.
Renaming each version works, but the new name spreads. It's like writing "Asha B." on the new girl's hook: every list in the school must use the new name too. Stencil's docs on renaming tags warn you to be "thoughtful when writing CSS selectors".
Checking get() first, as in lesson 4.3, only hides the error. The explainer says "there is no way to determine if the definitions are compatible". Cup Club would quietly run with version 2.
What does new CustomElementRegistry() do?
It makes a new, empty registry. It works like the page's: you define() names in it, and look them up with get() and getName(). But nothing you put in it reaches the page's registry.
The browser makes the page's registry for you. You reach it through customElements. A registry you make with new CustomElementRegistry() is called scoped. MDN says its definitions "are not globally available". They only count where you attach the registry.
class TeaCup extends HTMLElement {}
const clubTags = new CustomElementRegistry();
clubTags.define("tea-cup", TeaCup);
console.log(clubTags.get("tea-cup") === TeaCup);
console.log(clubTags.getName(TeaCup));
console.log(customElements.get("tea-cup"));
console.log(customElements.getName(TeaCup));- Line 1 makes a class for a tea cup.
- Line 3 makes a new registry,
clubTags. Line 4 definestea-cupin it. - Line 6 prints
true:clubTagsfinds the class by its name. Line 7 printstea-cup.getName()goes the other way, from class to name, as lesson 4.1 showed. - Line 8 asks the page's registry for
tea-cup. It printsundefined. Line 9 printsnull.
Why it matters: a scoped define() never touches the page. It can't clash with the page, and the page can't see it.
The rules inside are the ones from lesson 3.1. A second define() for tea-cup in clubTags throws a NotSupportedError too. Each registry holds each name once. It's only that there are now two lists.
A food court has a big sign by the door, and a menu at each counter. At the tea counter, "house blend" is Assam with milk. At the juice counter, "house blend" is apple and ginger. Each counter's menu decides what the name means there. The big sign lists neither.
- In real life: The food court
- In JavaScript: The page
- In real life: The big sign by the door
- In JavaScript: The page's registry,
customElements - In real life: One counter's own menu
- In JavaScript: A registry from
new CustomElementRegistry() - In real life: "House blend"
- In JavaScript: A tag name, like
tea-cup
whenDefined() works per registry too. It gives a promise that fulfills when that registry defines the name:
const clubTags = new CustomElementRegistry();
clubTags.whenDefined("tea-cup").then(() => console.log("Cup Club has tea-cup"));
customElements.whenDefined("tea-cup").then(() => console.log("The page has tea-cup"));
clubTags.define("tea-cup", class extends HTMLElement {});- Line 3 waits for
tea-cupinclubTags. Line 4 waits for it in the page's registry. - Line 6 defines
tea-cupinclubTagsonly.
It prints Cup Club has tea-cup, and nothing else. The page's promise is still waiting. Exercise 3 shows the bug this can cause.
How do you give a shadow root its own registry?
Pass the registry to attachShadow(), in the customElementRegistry option. Elements parsed inside that shadow root then look their names up in your registry. Lesson 6.1 showed attachShadow(), the method that gives an element a shadow root.
<tea-button label="Add to cart"></tea-button><div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; const redeem = root.querySelector("tea-button"); console.log(redeem.textContent); console.log(redeem.customElementRegistry === clubTags); console.log(root.customElementRegistry === clubTags); console.log(club.customElementRegistry === customElements);</script>- Line 1 is the shop's button. Line 2 is an empty box for Cup Club.
- Line 5 loads the shop's file, so the page's registry has version 2.
- Line 6 imports Cup Club's version 1 class. In the
cup-club/tea-button.jstab, this copy only exports its class. It doesn't calldefine(). - Lines 8 and 9 make
clubTagsand definetea-buttonin it, with version 1. - Line 12 gives the box a shadow root that uses
clubTags. Line 13 writes a<tea-button>into that root.
The result shows Add to cart (v2) and Redeem (v1). That's two versions of one tag on one page, with no error. Lines 16 to 19 print Redeem (v1), then true three times.
Lines 17 to 19 read a property called customElementRegistry. Elements, shadow roots, and documents all have it. It tells you which registry builds that node's custom elements:
- The button's is
clubTags. It was parsed inside the root, so it got the root's registry. - The root's is
clubTagstoo, from line 12. - The host's,
club, iscustomElements. The host lives in the page, so it keeps the page's registry.
Leaf and Kettle has a head office and a warehouse. In the head office, extension 21 rings Ravi. In the warehouse, extension 21 rings Asha. The number is the same. Each building's phone list decides who answers.
- In real life: A building
- In JavaScript: A tree: the page, or a shadow root
- In real life: The building's phone list
- In JavaScript: That tree's registry
- In real life: Dialing 21
- In JavaScript: Writing
<tea-button> - In real life: Who answers
- In JavaScript: The class that builds it
Making elements from a script
innerHTML isn't the only way in. document.createElement() takes an option called customElementRegistry. It decides which registry builds the new element. Try four ways:
<div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); const button = document.createElement("tea-button"); button.setAttribute("label", "Redeem"); document.body.append(button); console.log(button.textContent); console.log(button.customElementRegistry === clubTags);</script>createElement() without the option uses the page's registry. The button is built from version 2 at once, on line 12, before it goes anywhere.
Without the option, the button is version 2, even inside Cup Club's root. With it, the button is version 1, even out in the page. An element gets its registry when it's made, and keeps it wherever it goes. MDN puts it plainly: once set, "it cannot be changed".
The old explainer, and the polyfill later in this lesson, use a different method: shadowRoot.createElement(). It isn't in the DOM Standard. In Chrome, a shadow root has no createElement() at all. Use document.createElement() with the option.
Upgrades inside the scope
A tag can wait on the page before its class arrives, as lesson 4.1 showed. Inside a scoped root, it waits for the root's registry, not the page's:
<div id="club"></div> <script type="module"> import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; const redeem = root.querySelector("tea-button"); customElements.define("tea-button", class extends HTMLElement {}); console.log(redeem.matches(":defined")); clubTags.define("tea-button", TeaButton); console.log(redeem.matches(":defined"), redeem.textContent);</script>- Line 6 makes
clubTags, still empty. Lines 8 and 9 give the box a root with it, and write a button inside. - Line 12 defines
tea-buttonin the page's registry. Line 13 printsfalse. The button in Cup Club's root is still not defined.:definedmatches custom elements that have been upgraded, as lesson 2.4 showed. - Line 15 defines
tea-buttoninclubTags. Line 16 printstrue Redeem (v1). The waiting button upgraded at once.
Why it matters: a widget can define its tags late, after its root is on the page. And the page's own define() calls can never upgrade the widget's elements by mistake.
Can two versions of a web component share one page?
Yes. Give the older version a registry of its own, in the shadow root of the component that uses it. Here is the opening checkout again. The page is the same, line for line. Only Cup Club's two files changed:
<tea-button label="Add to cart"></tea-button><cup-club></cup-club> <script type="module"> await import("./tea-button.js"); try { await import("./cup-club/cup-club.js"); } catch (error) { console.log(`${error.name}: ${error.message}`); }</script>In the cup-club/tea-button.js tab, the last line is gone. Cup Club's copy of version 1 no longer calls define(). It only exports its class. In the cup-club/cup-club.js tab:
- Line 1 imports that class.
- Lines 3 and 4 make Cup Club's registry,
clubTags, and definetea-buttonin it. This runs once, when the file loads. - Line 9 gives each
cup-cluba shadow root that usesclubTags. - Lines 10 and 11 write the widget's text and its Redeem button into that root.
The result shows Add to cart (v2), then the Cup Club box with Redeem (v1). The console prints nothing: line 8 of the page loaded without an error.
tea-button is built from the registry of the tree it lives in. The page's button uses customElements. The one inside Cup Club's shadow root uses clubTags.That's what the Chrome team's post promised: "multiple versions of the same library side-by-side". The shop never changed a line.
One thing doesn't carry over by itself. Say a component inside Cup Club's root calls attachShadow() with no option. Its new root gets the page's registry, not clubTags. The DOM Standard starts each new shadow root with its document's registry, unless the option says otherwise. So every component that should use clubTags must pass it on. Exercise 6 shows how.
What does CustomElementRegistry.initialize() do?
It gives a registry to a root that doesn't have one yet. You need it when the root exists before your script runs. That happens with declarative shadow DOM: a <template shadowrootmode> in the HTML, which the browser turns into a shadow root while it reads the page. Lesson 2.5 met it.
HTML can't point at a JavaScript object. So HTML has an attribute that means "leave the registry empty for now": shadowrootcustomelementregistry. A script fills the slot later:
<cup-club> <template shadowrootmode="open" shadowrootcustomelementregistry> <p>Cup Club: 2 free cups</p> <tea-button label="Redeem"></tea-button> </template></cup-club> <script type="module"> import { TeaButton } from "./cup-club/tea-button.js"; const root = document.querySelector("cup-club").shadowRoot; const redeem = root.querySelector("tea-button"); console.log(root.customElementRegistry); console.log(redeem.matches(":defined")); const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); clubTags.initialize(root); console.log(root.customElementRegistry === clubTags); console.log(redeem.textContent);</script>- Lines 1 to 6 are Cup Club's widget, sent as HTML. Line 2 makes the shadow root and leaves its registry empty.
- Line 13 prints
null: the root has no registry yet. Line 14 printsfalse: the button inside is waiting. - Lines 16 and 17 make
clubTags, with version 1. - Line 18 calls
clubTags.initialize(root). It sets the registry of the root, and of every element inside that has none yet, toclubTags. Then it upgrades what it can. - Line 19 prints
true. Line 20 printsRedeem (v1).
attachShadow() can leave the slot empty too: pass customElementRegistry: null. The root reads null until a script calls initialize().
At a wedding dinner, some seats have a card that says Reserved, with no name yet. Later the host writes a guest's name on it, in ink. Once a name is on the card, it stays. A second host can't write over it.
- In real life: The blank card
- In JavaScript: A root whose registry is
null - In real life: Writing a name in ink
- In JavaScript:
initialize() - In real life: A card with a name on it
- In JavaScript: A root that already has a registry
- In real life: The second host's pen
- In JavaScript: A second
initialize(), which changes nothing
That last pair is the part people get wrong. initialize() only fills a slot that is empty. Try it on three roots:
<cup-club> <template shadowrootmode="open" shadowrootcustomelementregistry> <tea-button label="Redeem"></tea-button> </template></cup-club> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const root = document.querySelector("cup-club").shadowRoot; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); // Nothing else fills the root's registry. clubTags.initialize(root); console.log(root.customElementRegistry === clubTags); console.log(root.querySelector("tea-button").textContent || "Not upgraded");</script>The shadowrootcustomelementregistry attribute on line 2 leaves the root's registry empty. Line 15 fills it with clubTags and upgrades the button.
Only An empty slot prints true. The other two print false, and nothing throws. With The page's registry, the parser gave the root customElements, so clubTags can't replace it. With Filled first by another registry, line 14 took the empty slot, and the button never upgrades.
initialize() fills a root's registry only while it's empty. Once clubTags is there, a second initialize() changes nothing, and no error says so.There's no other way to swap a root's registry. The customElementRegistry property only has a getter. In a module script, assigning to it throws a TypeError. The HTML Standard says a registry, once set, "intentionally cannot be changed any further."
Read each row across: what you write, the registry the root gets, and whether initialize() can fill it later.
| When you write | The root's registry | Can initialize() fill it later? |
|---|---|---|
attachShadow({ mode: "open" }), or <template shadowrootmode> | The page's registry, customElements. | No. It already has one. |
attachShadow({ mode: "open", customElementRegistry: clubTags }) | clubTags, from the start. | No. It already has one. |
customElementRegistry: null, or the shadowrootcustomelementregistry attribute | None yet. It reads null. | Yes, once. After that it stays. |
So when does initialize() throw? MDN lists one case: a NotSupportedError, when a registry that isn't scoped gets a document, or a node whose document uses another registry. Here is the second kind:
const other = document.implementation.createHTMLDocument();
const box = other.createElement("div");
try {
customElements.initialize(box);
} catch (error) {
console.log(`${error.name}: ${error.message}`);
}- Line 1 makes a new, empty document. Its registry is
null. - Line 2 makes a
<div>in it. Line 5 asks the page's registry to take it. - Line 7 prints
NotSupportedError: Failed to execute 'initialize' on 'CustomElementRegistry': The registry provided is a global registry from another document.
Why it matters: forget the attribute, and nothing warns you. The widget just runs with the page's classes. When a scoped widget looks wrong, check its root's customElementRegistry first.
Which browsers support scoped custom element registries?
All three engines, in their latest versions. Safari 26 shipped them first, in September 2025. Chrome 146 followed in March 2026, along with Edge. Microsoft's Edge team worked on the feature in Chromium. Firefox 159 completed the set on October 27, 2026. The shadowrootcustomelementregistry attribute shipped in the same versions.
That made them Baseline newly available on October 27, 2026. Baseline is a label for web features. Newly available means the latest Chrome, Edge, Firefox, and Safari all have it. It doesn't mean every visitor has it. Someone on Safari 18, or an older Chrome, doesn't. Widely available comes 30 months later. Lesson 1.2 explained the labels.
So check before you rely on it:
const scoped = "initialize" in CustomElementRegistry.prototype;
console.log(scoped ? "Scoped registries: yes" : "Scoped registries: no");Line 1 looks for initialize(), which shipped with the constructor in every browser. In Chrome, line 2 prints Scoped registries: yes.
Scoped registries are also an Interop 2026 focus area. Interop is a yearly project. Browser makers pick features, run the same tests, and fix what fails. The Interop 2026 dashboard shows how many of these tests pass in every browser at once.
Is there a polyfill for scoped custom element registries?
Yes: @webcomponents/scoped-custom-element-registry. A polyfill is code that adds a missing feature to older browsers. Its README calls it a "work in progress", based on a "non-finalized spec proposal".
It fakes scoping with stand-ins. For each tag name, it registers one stand-in class in the page's real registry. When an element is made, the stand-in finds the registry of the element's root, and hands over to the class there.
It follows the proposal's older shape. Its source, at version 0.0.11, differs from what browsers shipped:
- It reads the registry from an option called registry, not
customElementRegistry. - It adds shadowRoot.createElement(). Browsers have
document.createElement()with an option instead. - It has no
initialize(). And it always replaces the browser's ownCustomElementRegistry, even where the real one exists. Its changelog says "polyfill always used".
What does that mean for your code? Code written for the polyfill scopes nothing in a browser with the real feature:
const clubTags = new CustomElementRegistry();
const host = document.createElement("div");
const root = host.attachShadow({ mode: "open", registry: clubTags });
console.log(root.customElementRegistry === customElements);
console.log("createElement" in root);- Line 3 passes the registry the polyfill's way, as
registry. - Line 5 prints
true. Chrome ignored the option, so the root uses the page's registry. - Line 6 prints
false. There's nocreateElement()on a shadow root.
And code written for the standard scopes nothing while the polyfill is loaded, because the polyfill never reads customElementRegistry. So a site can't just add the polyfill and keep writing the standard API. The lesson on which polyfills you still need, in Testing, tooling, and publishing, covers loading one only where it's missing.
One more fact, for a sense of scale. The first issue asking for scoped registries, WICG issue #716, was opened by Justin Fagnani in December 2017. They became Baseline in October 2026, nearly nine years later.
Can the tea kit go in a scoped registry?
Not as it is. Try it. This page loads today's tea kit. Then Cup Club brings its own copy of the kit as it was after lesson 3.1, in a folder of its own:
<tea-card name="Oolong" price="5"></tea-card> <script type="module"> await import("./tea-kit/index.js"); try { await import("./cup-club/tea-kit/index.js"); } catch (error) { console.log(`${error.name}: ${error.message}`); }</script>- Line 1 is a tea card, from today's kit.
- Line 4 loads the whole kit.
tea-kit/index.jsimports every component file. - Line 7 loads Cup Club's older copy, from
cup-club/tea-kit/.
The card shows Oolong and Price: 5. Line 9 prints NotSupportedError: Failed to execute 'define' on 'CustomElementRegistry': the name "tea-badge" has already been used with this registry. The error names tea-badge, a tag this page doesn't even use. Cup Club's copy failed on the first file its index loads.
A scoped registry can't fix this from outside. Open the tea-kit/tea-badge.js tab. Its last line is customElements.define("tea-badge", TeaBadge);. Each kit file registers itself in the page's registry the moment it loads. By the time Cup Club could hand it a registry, the clash has already happened.
A tree planted in the front garden stays there. If the back room wants one, it can't borrow it. A plant in a pot is different. You carry it to the window, the counter, or the back room, wherever it's needed.
- In real life: A tree planted in the front garden
- In JavaScript: A file that calls
customElements.define()itself - In real life: The front garden
- In JavaScript: The page's registry
- In real life: A plant in a pot
- In JavaScript: A file that only exports its class
- In real life: Choosing the spot
- In JavaScript: The page calling
define()on the registry it wants
The fix belongs in the library. Open Web Components' docs say the modules holding your classes should be "side effect free" and "don't call customElements.define themself". The page that uses the library decides where each class is defined. Cup Club's version 1 file was already built that way.
A library can offer both. It ships one file per component that only exports its class, and one small file that defines them all in the page's registry for everyone else. The tea kit keeps lesson 1.4's rule for now: each file registers its own tag. The lesson on organizing a component library, in Design systems, covers registration and export patterns. The lesson on micro-frontends, in Applications and performance, uses scoped registries to keep teams apart.
Common mix-ups
- "Two versions of the same design system can't share a page." No. That was true until scoped registries reached every engine in 2026. Give the older version a registry of its own, as Cup Club did.
- "Scoped registries use shadowRoot.createElement()." No. That's the old explainer's and the polyfill's shape. Browsers shipped
document.createElement(name, { customElementRegistry }). Chrome has nocreateElement()on a shadow root. - "You can swap a root's registry later." No.
initialize()only fills an empty slot, and the property only has a getter. Once set, a root's registry stays. - "initialize() throws when the root already has a registry." No. It skips that root, with no error. That's why the mistake is easy to miss.
- "Defining into a scoped registry also defines it globally." No.
customElements.get("tea-cup")gaveundefinedfor a name onlyclubTagshad. - "Components inside a scoped root pass the registry on to their own shadow roots." No.
attachShadow()without the option uses the page's registry. PasscustomElementRegistryeach time. - "Baseline means every visitor has it." No. Scoped registries are newly available. Older browsers that people still use don't support them.
Practice exercises
Answer first, then use the hints. Each result runs live, so you can check yourself.
What does the console print?
const clubTags = new CustomElementRegistry();
clubTags.define("tea-cup", class extends HTMLElement {});
console.log(clubTags.get("tea-cup") === customElements.get("tea-cup"));false. clubTags gives the class. The page's registry never heard of tea-cup, so it gives undefined.
What does the console print?
<tea-button label="Add to cart"></tea-button><div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); const root = document.querySelector("#club").attachShadow({ mode: "open" }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; console.log(root.querySelector("tea-button").textContent);</script>Redeem (v2). Line 11 never passes clubTags, so the root gets the page's registry. clubTags is made and filled, but nothing uses it.
Nothing prints. Line 10 should print the button's text once Cup Club's class arrives. Which registry should it wait on?
<div id="club"></div> <script type="module"> const clubTags = new CustomElementRegistry(); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; const redeem = root.querySelector("tea-button"); customElements.whenDefined("tea-button").then(() => console.log(redeem.textContent)); const { TeaButton } = await import("./cup-club/tea-button.js"); clubTags.define("tea-button", TeaButton);</script><div id="club"></div> <script type="module"> const clubTags = new CustomElementRegistry(); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; const redeem = root.querySelector("tea-button"); clubTags.whenDefined("tea-button").then(() => console.log(redeem.textContent)); const { TeaButton } = await import("./cup-club/tea-button.js"); clubTags.define("tea-button", TeaButton);</script>Wait on clubTags. The page's registry never gets tea-button, so its promise never fulfills. Cup Club's does, and the page prints Redeem (v1).
This code follows the old explainer. Fix line 12. What does the page print then?
<div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); const button = root.createElement("tea-button"); button.setAttribute("label", "Redeem"); root.append(button); console.log(button.textContent);</script><div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); const button = document.createElement("tea-button", { customElementRegistry: clubTags }); button.setAttribute("label", "Redeem"); root.append(button); console.log(button.textContent);</script>It prints Redeem (v1). shadowRoot.createElement() came from the old proposal, and it isn't in the standard. The option on document.createElement() gives the button Cup Club's registry.
Cup Club's Redeem button shows version 2. Change only line 3 so it shows version 1. Which attribute did you add?
<tea-button label="Add to cart"></tea-button><cup-club> <template shadowrootmode="open"> <tea-button label="Redeem"></tea-button> </template></cup-club> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const root = document.querySelector("cup-club").shadowRoot; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); clubTags.initialize(root); console.log(root.querySelector("tea-button").textContent);</script><tea-button label="Add to cart"></tea-button><cup-club> <template shadowrootmode="open" shadowrootcustomelementregistry> <tea-button label="Redeem"></tea-button> </template></cup-club> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; const root = document.querySelector("cup-club").shadowRoot; const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); clubTags.initialize(root); console.log(root.querySelector("tea-button").textContent);</script>With the attribute, the root's registry starts as null, so line 15 can fill it. The page prints Redeem (v1), and the shop's button stays Add to cart (v2).
What does the page print? Then change line 10 so the card's button is version 1.
<div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; class ClubCard extends HTMLElement { constructor() { super(); const root = this.attachShadow({ mode: "open" }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; } } const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); clubTags.define("club-card", ClubCard); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<club-card></club-card>`; const card = root.querySelector("club-card"); console.log(card.shadowRoot.querySelector("tea-button").textContent);</script><div id="club"></div> <script type="module"> import "./tea-button.js"; import { TeaButton } from "./cup-club/tea-button.js"; class ClubCard extends HTMLElement { constructor() { super(); const root = this.attachShadow({ mode: "open", customElementRegistry: this.customElementRegistry }); root.innerHTML = `<tea-button label="Redeem"></tea-button>`; } } const clubTags = new CustomElementRegistry(); clubTags.define("tea-button", TeaButton); clubTags.define("club-card", ClubCard); const club = document.querySelector("#club"); const root = club.attachShadow({ mode: "open", customElementRegistry: clubTags }); root.innerHTML = `<club-card></club-card>`; const card = root.querySelector("club-card"); console.log(card.shadowRoot.querySelector("tea-button").textContent);</script>Before the fix, it prints Redeem (v2). The card itself is built from clubTags, but its new root starts with the page's registry. Passing this.customElementRegistry hands clubTags on, and it prints Redeem (v1).
Check your understanding
For each question, ask which registry builds the element, and when that registry was set.
Question 1 of 8In the opening checkout page, what does the console print?
Read the code, then predict<tea-button label="Add to cart"></tea-button> <cup-club></cup-club> <script type="module"> await import("./tea-button.js"); try { await import("./cup-club/cup-club.js"); } catch (error) { console.log(`${error.name}: ${error.message}`); } </script>Choose an answer to see the explanation.
Question 2 of 8In the first playground, Cup Club's file loads first. What does the shop's own button show?
Choose an answer to see the explanation.
Question 3 of 8What does line 8 of the registry example print?
Read the code, then predictclass TeaCup extends HTMLElement {} const clubTags = new CustomElementRegistry(); clubTags.define("tea-cup", TeaCup); console.log(clubTags.get("tea-cup") === TeaCup); console.log(clubTags.getName(TeaCup)); console.log(customElements.get("tea-cup")); console.log(customElements.getName(TeaCup));Choose an answer to see the explanation.
Question 4 of 8A button is made with
document.createElement("tea-button", { customElementRegistry: clubTags })and added todocument.body. Which version builds it?Choose an answer to see the explanation.
Question 5 of 8A shadow root already uses
clubTags. What doesotherTags.initialize(root)do?Choose an answer to see the explanation.
Question 6 of 8What does the
shadowrootcustomelementregistryattribute do?Choose an answer to see the explanation.
Question 7 of 8In Chrome, what does line 5 of the polyfill example print?
Read the code, then predictconst clubTags = new CustomElementRegistry(); const host = document.createElement("div"); const root = host.attachShadow({ mode: "open", registry: clubTags }); console.log(root.customElementRegistry === customElements); console.log("createElement" in root);Choose an answer to see the explanation.
Question 8 of 8Scoped registries are Baseline newly available. What does that mean for your visitors?
Choose an answer to see the explanation.
Key takeaways
- The page's registry holds each tag name once. A second
define()throws aNotSupportedError, and the first class wins. - Prefixes don't help two versions of one library. Renamed tags spread through your CSS and scripts.
new CustomElementRegistry()makes an empty, scoped registry. Its names never reachcustomElements.attachShadow({ mode, customElementRegistry })gives a shadow root its own registry. Elements parsed inside use it.document.createElement(name, { customElementRegistry })sets an element's registry when it's made, and the element keeps it wherever it goes.shadowrootcustomelementregistryleaves a declarative root's registry empty.initialize()fills an empty slot once, and never swaps a registry.- Scoped registries are Baseline newly available since October 27, 2026. The polyfill uses an older shape:
registryandshadowRoot.createElement(). - A library is ready for scoping when its files only export their classes.
Remember the one-liner.
A scoped registry is a second list of tag names that only one shadow root uses, so the same name can build a different class there.
Sources
The facts on this page come from these standards, docs, and source files:
- Using custom elements: Scoped custom element registries, MDN.
- Make custom elements behave with scoped registries, Jayson Chen and Patrick Brosset, Chrome for Developers.
- Scoped Custom Element Registries explainer, Justin Fagnani, WICG.
- Tag Transformation, Stencil docs.
- CustomElementRegistry() constructor, MDN.
- HTML Standard: Scoped custom element registries, WHATWG.
- DOM Standard: attachShadow(), WHATWG.
- Element: customElementRegistry property, MDN.
- ShadowRoot: customElementRegistry property, MDN.
- Document: createElement() method, MDN.
- Scoped Elements, Open Web Components.
- CustomElementRegistry: initialize() method, MDN.
- HTML Standard: initialize(), WHATWG.
- <template>: shadowrootcustomelementregistry, MDN.
- Firefox 159 release notes for developers, MDN.
- Interop 2026 dashboard, web-platform-tests.
- Scoped CustomElementRegistry polyfill, Web Components polyfills.
- WICG/webcomponents issue #716: Scoped Custom Element Registries, Justin Fagnani.
That ends the module on shadow roots. Coming next: The template element, the next lesson in Shadow DOM, templates, and slots. Write markup once in a <template>, and stamp out copies quickly and safely.