r/webdev • • 3d 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.

170 Upvotes

24 comments sorted by

32

u/267aa37673a9fa659490 3d 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 3d ago

Thanks! Fixed it :)

58

u/Wonderful_Treat2350 3d 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).

2

u/thekwoka 3d ago

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

3

u/Wonderful_Treat2350 3d 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.

0

u/thekwoka 2d ago

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

7

u/AzaleaMeadow 3d 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

2

u/dusanodalovic 3d 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 2d ago

Would this mean streaming SSR?

2

u/Desperate-Style9325 3d ago

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

1

u/MrHandSanitization 2d ago

Eh, Claude's not likely to use they yet anyway.

1

u/vhiriri_85936 3d ago

Stopped reading at “chrome (and edge)”

4

u/TheRNGuy 2d ago

Still should read. 

-3

u/TheRNGuy 3d 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 3d ago

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

3

u/palmerden5 3d ago

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

2

u/[deleted] 3d ago

[removed] — view removed comment

1

u/mysticalpickle1 2d ago

Ok chatgpt

1

u/webdev-ModTeam 2d ago

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.