Flash of Undefined Custom Elements (FOUCE)
Tags: webcomponents, css, javascript
Published: 2021-12-29 17:15:00

Web components are [defined and registered](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define) with JavaScript. Depending on how and when you load the scripts that perform registration, you may see a brief flash of unstyled HTML where your custom elements should be when the page loads. This is not dissimilar to [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content), which occurs when HTML is displayed before the stylesheet has loaded.

For reference, here's an exaggerated example of three custom elements loading at different intervals.

![Exaggerated example of FOUCE as custom elements load one by one](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8to9lnborhq6mn7gahhk.gif)

Since the world needs more acronyms, and since one doesn't seem to exist yet, I'm calling this phenomenon FOUCE (rhymes with "spouse"), which stands for _Flash of Undefined Custom Elements_.

Fortunately, the browser gives us some tools to mitigate it.

## The `:defined` selector

One option is to use the [`:defined`](https://developer.mozilla.org/en-US/docs/Web/CSS/:defined) CSS pseudo-class to "hide" custom elements that haven't been registered yet. You can scope it to specific tags or you can hide all undefined custom elements as shown below.

```css
:not(:defined) {
  visibility: hidden;
}
```

As soon as a custom element is registered, it will immediately appear with all of its styles, effectively eliminating FOUCE. Note the use of `visibility: hidden` instead of `display: none` to reduce shifting as elements are registered.

The drawback to this approach is that custom elements can potentially appear one by one instead of all at the same time.

![FOUCE is gone, but elements can appear at random as they are defined](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/243qaoljsjav4an724xe.gif)

That's certainly a lot better, but can we take things a bit further?

## Awaiting `customElements.whenDefined()`

Another option is to use [`customElements.whenDefined()`](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/whenDefined), which returns a promise that resolves when the specified element gets registered. You'll probably want to use it with [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) in case an element fails to load for some reason ([thanks, Westbrook!](https://twitter.com/WestbrookJ/status/1476286720278683650?s=20)).

A clever way to use this method is to hide the `<body>` with `opacity: 0` and add a class that fades it in as soon as all your custom elements are defined.

```html
<style>
  body {
    opacity: 0;
  }

  body.ready {
    opacity: 1;
    transition: .25s opacity;
  }
</style>

<script type="module">
  await Promise.allSettled([
    customElements.whenDefined('my-button'),
    customElements.whenDefined('my-card'),
    customElements.whenDefined('my-rating')
  ]);

  // Button, card, and rating are registered now! Add
  // the `ready` class so the UI fades in.
  document.body.classList.add('ready');
</script>
```

In my opinion, this is the better approach because it subtly fades in the entire page as soon as all your custom elements are registered. After all, what's the point of showing the page before it's ready?

![No more FOUCE because the body fades in only after all elements are defined](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ky3dfttnpddx5lv40t60.gif)

The drawback, of course, is that you need to keep track of which elements you're using and add them to the list. But this can also be an advantage if your initial UI only requires a handful of custom elements. For example, you can load only the ones you need upfront and let the rest of them load asynchronously to make your page load faster.

Have you used either of these methods to prevent FOUCE? Have you thought of a better way? [Let me know on Twitter!](https://twitter.com/claviska)

---

<small>
  December 30, 2021: The original version of this article mentioned script placement in the `<head>` as a method of eliminating FOUCE, but that doesn't work if you're using ES modules. While the approach works for non-modules, I've removed it because it leads to poor page load times and because of the growing ubiquity of ES modules on the Web.
</small>