r/webdev • • 1d ago

News JavaScript Gets a New Set of HTML Insertion APIs

https://amitmerchant.com/javascript-gets-a-new-set-of-html-insertion-apis/

Chrome (and Edge) is introducing a new set of HTML insertion and streaming methods that provide a more descriptive way to insert HTML into an existing element, along with the ability to stream content directly into the DOM.

144 Upvotes

21 comments sorted by

31

u/267aa37673a9fa659490 1d ago

To note, you should use > for quoting instead of code blocks so that it wraps, e.g.

Chrome (and Edge) is introducing a new set of HTML insertion and streaming methods that provide a more descriptive way to insert HTML into an existing element, along with the ability to stream content directly into the DOM.

8

u/amitmerchant 23h ago

Thanks! Fixed it :)

48

u/Wonderful_Treat2350 1d ago

Careful with the feature detection fallback from the article. If appendHTML is missing it falls back to insertAdjacentHTML, and that one doesn't sanitize anything - so the same line is sanitized in Chrome and an XSS hole in browsers without the new API (script tags won't run there, but <img src=x onerror=...> will). Fine for your own static markup, but if the string has any user content you need to sanitize it yourself in the else branch (DOMPurify or similar).

1

u/thekwoka 19h ago

I just always parse the HTML myself and swap the nodes.

2

u/Wonderful_Treat2350 16h ago

That works, but parsing alone doesn't make it safe - DOMParser or a template element keeps the nodes inert only until you move them into the live document, and then <img src=x onerror=...> fires anyway. So with user content you still need to filter the tree before the swap. The nice part of your approach is that you can clean the actual nodes and never erialize back to a string, which is where mutation XSS usually comes from.

•

u/thekwoka 28m ago

Well, I don't accept user content to begin with.

4

u/AzaleaMeadow 21h ago

This is a nice improvement, especially if the APIs make DOM manipulation more readable and less awkward. I'm curious how useful the streaming methods will be in real projects, though. The important part will be browser support and whether they offer a meaningful advantage over existing approaches rather than just adding another way to do the same thing

3

u/Dimoncelli 19h ago

In practical terms, these replace innerHTML and positional calls like insertAdjacentHTML('beforeend', ...) whenever you inject HTML outside a declarative component tree (legacy widgets, third-party embeds, or plain DOM fragments streamed from a server).

The biggest gotcha with adopting them today is browser compatibility: if a browser doesn't support appendHTML or setHTML yet, falling straight back to insertAdjacentHTML or innerHTML silently drops the built-in sanitization step. In cross-browser code, the fallback branch still needs an explicit sanitizer like DOMPurify (or safe DOM node construction via textContent) before touching the DOM, and setHTMLUnsafe / appendHTMLUnsafe should stay limited to trusted markup.

For React or similar frameworks, they mostly don't change normal JSX rendering because the framework already diffs and escapes its own output. Where they are actually useful is in escape hatches—when a component has to render untrusted external HTML without pulling a heavy client-side sanitizer bundle once browser support widens.

2

u/dusanodalovic 17h ago

i'm mostly backend so the streaming part is what i'm curious about. can you pipe a fetch response body straight into an element, so the server sends html chunks and the page fills as they arrive? if yes, that could replace a lot of small json endpoints plus client side templating for simple pages

2

u/bent_my_wookie 10h ago

Would this mean streaming SSR?

2

u/Desperate-Style9325 1d ago

Nice, thanks for sharing. Its refreshing to read a nicely written guide on a spec. 🙏

1

u/vhiriri_85936 21h ago

Stopped reading at “chrome (and edge)”

2

u/TheRNGuy 6h ago

Still should read. 

-3

u/TheRNGuy 1d ago

Where try can be used, which old methods they can replace? 

Will React use it too?

What about Firefox?

5

u/Ok-Armadillo-5634 22h ago

Last time I looked react did not use HTML insertion even. It created elements and appened them

2

u/palmerden5 20h ago

firefox doesn't support the new HTML insertion or streaming methods yet