I have an element that I want to populate as a request's HTML streams in, instead of waiting for the complete response. This turned out to be incredibly difficult.
Things I've tried:
1. milestoneNode.insertAdjacentHTML('beforebegin', text)
Be lovely if this worked. Unfortunately, elements with quirky parsing wreck it — such as <p>
and <table>
. The resulting DOM can be charitably described as Dada.
2. Using a virtual DOM/DOM update management library
Google's incremental-dom
seemed most promising, but its patch()
operation always restarts from the beginning of the container node. Not sure how to "freeze" it in place.
This also has baggage from doing at least HTML tokenization in JavaScript, and some actual tree-building would have to happen, unless one serves well-formed XHTML5. (Nobody does.) Reimplementing a browser's HTML parser seems like a sign I've gone horribly wrong.
3. document.write()
I was desperate. Ironically, this ancient boogeyman has almost the behavior I need, sans the "throwing away the existing page" thing.
4. Appending to a string, then innerHTML
ing it periodically
Defeats the point of streaming, since eventually the entire response gets held in memory. Also has repeated serialization overhead.
On the plus side, it actually works. But surely there's a better way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…