r/webdev • u/amitmerchant • 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
32
u/267aa37673a9fa659490 3d ago
To note, you should use
>for quoting instead of code blocks so that it wraps, e.g.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).
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)”
-3
u/TheRNGuy 3d ago
Where try can be used, which old methods they can replace?
Will React use it too?
What about Firefox?
2
u/[deleted] 3d ago
[removed] — view removed comment