https://lamplightdev.com/blog/2024/01/10/streaming-html-out-of-order-without-javascript/ lamplightdev * Home * Blog * Contact * Newsletter Streaming HTML out of order without JavaScript Wed Jan 10 2024 Let's start with a demo: https://ooo.lamplightdev.workers.dev: This is a simple page that renders a list of 10 items. Try it with and without JavaScript enabled in your browser. There's a few things to notice: 1. The 'app shell' renders first - you see the header and the footer, but there's a loading placeholder where the list of items will be rendered. 2. After a second the loading placeholder is replaced with the list of items - but with each item itself having a loading placeholder. 3. The content of the items then renders out of order, replacing the loading placeholders - you see item 5 first, then the other items as they are generated. 4. If you look at the page source you'll see that the html is in the order it was sent - not the order it was rendered in 5. The page makes use of Shadow DOM without Custom Elements. Pretty nice, right? It may be a contrived example but it's an interesting technique that enables things that have not been possible before without JavaScript. Have a look at the code for this demo, or read on for an explanation of how it works. --------------------------------------------------------------------- Background Streaming HTML The concept of streaming HTML - sending HTML from a web server to a browser in chunks as it is generated - is nothing new. It seemed to take a back seat at the beginning of the age of modern front-end frameworks and Single Page Applications - where the entire page was generated in the browser - but as the pendulum swings back towards server-side rendering with full stack frameworks, streaming responses are becoming popular again. The advantages of streaming HTML over waiting for the entire response to be generated before sending it to the browser are clear - you can render something immediately to indicate to the user that something is happening, and you can start downloading assets like CSS and JavaScript earlier, while you wait for the more time consuming parts of the response to be generated. What's been lacking up to this point is a way to stream HTML out of order - that is to stream the HTML in chunks as it's generated without worrying about the order in which the chunks are sent to the browser - and still have the browser render the chunks of HTML in the correct order as in the demo above. Modern full stack frameworks enable this functionality by using a variety of clever techniques, all of which require buy-in to the particular framework and a hefty chunk of JavaScript. That might be fine for your use case, but what if we could achieve the same thing without any JavaScript or framework? Well now you can. Shadow DOM Shadow DOM is a way to render a piece of DOM in isolation from the rest of the page. Whilst often associated with Custom Elements, Shadow DOM can be used with any HTML tag, such as the humble
Hi ${name}!
`)}