Description
Recently, my team (within Chrome) has been working on initial explainers and explorations for new HTML elements: a virtual scroller, a toggle switch and a toast. There are a couple common issues we're attempting to tackle with these that need a central place to discuss, and the whatwg/html community is a good place to have those. These issues are polyfillability, and pay-for-what-you-use. They are somewhat connected, but I want to try to use two separate issues to discuss them; we'll see if that works. This issue is about pay for what you use.
The problem: adding new features to the global namespace, including HTML elements, increases the weight of every web page. Each individual feature is not very costly, but together they create a tragedy of the commons. The fact that features baked in to browsers are loaded for every page creates an attitude that more features belong in libraries, and less in the platform. But this leads to a fundamentally unergonomic platform, where you have to pull in large libraries (of inconsistent quality or accessibility) to accomplish basic UI patterns (such as, but clearly not limited to, virtual scrollers, toggle switches, toasts, etc.). We're hoping that by finding a new way to make HTML elements (and other APIs) pay-for-what-you-use, we can break the web out of this paradigm.
Elsewhere on the platform, we're exploring a solution for this problem via built-in modules, imported via the JavaScript module system. For TC39 specs, the JavaScript standard library proposal is meant to power APIs such as temporal. For web specs, the Web IDL modules infrastructure is meant to power specs such as KV storage. Overall, I think there is general interest from both the browser and implementer community in built-in modules as a solution for new JavaScript APIs.
This discussion is about how we can accomplish the same for HTML elements, not just JavaScript APIs. My opinion is that we can use the same solution as the rest of the platform. That is, you can opt in to using a HTML element using the JavaScript module system, e.g.
<script type="module">
import { StdSwitchElement } from "std:switch";
document.body.append(new StdSwitchElement());
</script>
or
<script type="module" src="std:switch"></script>
<std-switch></std-switch>
(Note: check also the polyfillability discussions in #4696, for the <std-
prefix.)
Apart from the alignment-with-other APIs question, the module system works well for this because it already has so much built-in: polyfilling via import maps (module the #4696 discussions), lazy-loading via import()
, etc. In the past, discussions around adding new systems for loading code, such as HTML imports, have specifically gotten push back from vendors who prefer to use the JavaScript module system as the unifying way to load dependencies.
But, what do others think?