- Declarative Shadow DOM
- IntersectionObserver
- setHTMLUnsafe()
- Sync & async JSX
Lazy loading with Declarative Shadow DOM
This page is mostly static text, which arrives in the first response. The widgets in between are only placeholders at first: each one is rendered on the server and fetched when you scroll to it.
- Placeholderskeleton in the first response
- In viewIntersectionObserver fetches it
- Renderedserver JSX with its shadow root
Static content first
Text, headings and images are cheap to render, and visitors and search engines want them right away. Widgets that need slow data or a lot of work are a different story: rendering them all up front would delay the whole page, even though most visitors never scroll down to see them.
So the page only contains a <Lazy name="clock" /> where a widget belongs. On the server, it renders a <lazy-load> element with a skeleton, like the one for the server time right here:
The clock is a sync component: a plain function that returns JSX right away. Its time is the moment the widget was requested, not when the page was loaded.
Slot fallback as skeleton
The <lazy-load> element has a declarative shadow root with a <slot>. The skeleton is the fallback content of that slot, so the browser shows it as long as the element has no children. No CSS class needs to be toggled when the content arrives.
Computing primes is sync too, but a slow async component doesn't hold up anything either. The weather below waits almost a second for its (simulated) backend:
Fetching shortly before it is visible
A small script defines the <lazy-load> element and watches it with an IntersectionObserver. 200 pixels before the element scrolls into view, it fetches /lazy-load/widget?name=…. That route renders the widget like any other JSX: it awaits whatever the component returns, JSX or a promise of JSX. The badge on each widget shows how long that took.
Every widget is a request of its own, so the stock ticker and the weather load in parallel when they are on screen together, and a slow widget never blocks a fast one.
Attaching the shadow root
The answer of the widget route contains its own <template shadowrootmode="open">. Plain innerHTML would ignore it and leave an inert template behind. setHTMLUnsafe() parses it like the HTML parser does, so the widget arrives with its shadow root attached and its styles encapsulated.
The new content becomes a child of <lazy-load>, the slot shows it instead of the skeleton, and the page is done. Until the next widget, that is:
Passing props
<Lazy> passes a props object on to the component. When the widget is loaded lazily, the props travel as JSON in the query string of the widget URL, so they have to be serializable: strings, numbers, booleans, arrays and objects.
<Lazy name="clock" props={{ timeZone: "Asia/Tokyo" }} />
<Lazy name="dice" props={{ count: 5 }} />Anyone can change a query string, so props from the browser are user input. Every component checks its own: the dice keep the count between 1 and 10, text props are limited in length, and an unknown time zone throws, which shows the error card from below. The same component works with different props, like a weather widget per city and a ticker with its own symbols:
When a component fails
Backends go down. The widget below calls a service which always throws after a second. The widget renderer catches the error, logs it on the server and sends a red error card instead. Only this widget is affected: the text around it and the other widgets are already on the page.
Reload the page and scroll down slowly to watch each widget load as it comes into view.