You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use-case: I work on a news site and I want to create visual transitions between articles, but I don't want to lose the benefits of streaming. So:
User clicks on link
Create a new article element to contain the incoming article
Start fetching content
Pipe content into new article element
Begin the visual transition once elements appear in the new article element
Not only is innerHTML is slower way to do this (due to a lack of streaming), it also introduces a number of behavioural differences. It'd be great to try to limit these, eg allow inline scripts to execute before additional elements are inserted.
Reacted by Taylor Hunt, Amelia Bellamy-Royds, Joshua Peek, Konrad Dzwinel, Andres Rios, Piotr Witek, Derek Petersen, Arihant Verma , GU Yiling, Mathias Bynens and 20 more
Reacted by Joshua Peek, Matei Stanca, Austin Armstrong, Ville V. Vanninen, AJ Klein, Bruce B. Anderson, Martin Auswöger, Oliver Williams, Anwar Hossain, Marieme Azib and 1 more
Hello! I'm curious about the possibility to insert scripts.
I'm trying to execute an script and I see the script is executed after insertion.
<!DOCTYPE html><body><script>varr=newRange();// Write some more content - this should be done async:document.body.appendChild(r.createContextualFragment('<p>hello'));document.body.appendChild(r.createContextualFragment(' world</p>'));document.body.appendChild(r.createContextualFragment('<script>console.log("yeap");<\/script>'));// done!!</script></body>
I've two questions:
What about if the inserted script has the flag... async or defer? Those flags will not have effect.
Will you keep this function in the standard? Or are you planning to remove it?
Finally I found (you gave me) the best way to insert content to the document.Thanks a lot!
@rianby64 note that the example above creates two paragraphs rather than one.
What about if the inserted script has the flag... async or defer? Those flags will not have effect.
The scripts will be async, as if you'd created them with document.createElement('script'). For the streaming solution I mentioned in the OP, I'd like the parser to queue DOM modifications while a non-async/defer script downloads and executes, but allow something like a look-ahead parser.
Will you keep this function in the standars?
Which function? createContextualFragment? I don't see why it'd be removed.
In general I think we want to be able to provide ReadableStream or Response objects to APIs that currently take a URL. @jakearchibald, would something that let you assign a ReadableStream or Response (backed by a stream) to an iframe.src satisfy your use case?
The key part here is to not have to use a separate iframe plus adoption of the current parser insertion point into a different document. Instead, we just want to parse into an existing document location.
This means creating an element that has the concept of partially loaded state, right? An iframe already has all of that, but do other html container elements? So wouldn't we need to create something that has all the load event, error event, and other stateful information of an iframe? Or maybe all that exists today. HTML always catches me out.
This means creating an element that has the concept of partially loaded state, right?
I think we can get away without this. If an element has a writable endpoint you'll get locking on that writable for free. However, during streaming you'll be able to modify the children of the element, even set the element's innerHTML. The HTML parser already has to deal with this during page load, so I don't think we need to do anything different.
So wouldn't we need to create something that has all the load event, error event, and other stateful information of an iframe?
We probably don't need this either. htmlStream.pipeTo(div.writable) - since pipeTo already returns a promise you can use that for success/failure info.
How would this interact / compare with the following scenario:
Rather than fetching HTML snippets from the server, I'm much more likely to be able to fetch [we'll assume newline-delimited to enable stream parsing] a minimal JSON encoding of whatever entity I'm trying to display.
Partially, this is just down to the fact that most web servers wrap HTML output in a series of filters, one of which is a base "<html><head/><body>...</body></html>" template. Obviously, that can [easily, from a purely technical perspective] change, but spitting out independent <div>s is going to take some cultural change on the server side. JSON we have today, and won't take any convincing.
... so, assuming that we use JSON, Is there a performance win to being able to render JSON snippets [as they come over the network] to HTML? The trade-off I'd assume we're making is on triggering additional layouts; put another way, is it faster to do:
[ JSON blob representing n divs ]: JSON -> HTML -> DOM in one step -or-
[ streaming JSON blob representing n divs ]: while (nextJSONitem) { JSON -> HTML -> DOM }
My expectation is that the answer is "it depends"; I don't have a sufficiently reliable playground for testing this to any degree of accuracy, but I would expect we'd want to keep the render pipeline as unobtrusive as possible while minimizing network->screen latency for individual items, using the following as trade-offs:
time to render & re-compute layout for:
each items in an array individually vs
all items in an array as a single DOM manipulation vs
batches of items
total time to:
fetch all items
pull a single item out of a streaming JSON blob
... ideally all while minimizing client complexity ("they wrote a lot of code to make things that slow"). Thankfully that part should be hidden in frameworks.
... OR am I totally barking up the wrong tree with the idea that JSON is the right delivery mechanism, and we should aim to generate server-side HTML snippets for pretty much anything that can be fetched-with-latency?
In #11964 we had consensus to advance this proposal to stage 2, with the specific scope of adding streamHTMLUnsafe() and adding the runScripts option to both streamHTMLUnsafe() and setHTMLUnsafe().
Reacted by Jake Archibald, Claudia Meadows and jlptom
Activity
jakearchibald commentedon Dec 7, 2016
Parts of this can be hacked using
document.writeand<iframe>, but it'd be good to have a non-hacky way.Code for the above use-case if
element.writableprovided a way to stream HTML into the element.zcorpan commentedon Dec 7, 2016
There is also https://w3c.github.io/DOM-Parsing/#idl-def-range-createcontextualfragment(domstring) which does execute scripts (when inserted into a document). As a possible alternative to
document.writein the meantime...jakearchibald commentedon Dec 7, 2016
TIL! I guess you mean an alternative to
innerHTML?zcorpan commentedon Dec 7, 2016
No, as an alternative to
document.writein your hack. Don't even need an iframe, just aRangeinstance.http://software.hixie.ch/utilities/js/live-dom-viewer/saved/4716
jakearchibald commentedon Dec 7, 2016
@zcorpan ah, that doesn't allow partial trees http://software.hixie.ch/utilities/js/live-dom-viewer/?saved=4717, which you kinda need if you're trying to stream something like an article.
rianby64 commentedon Dec 7, 2016
Hello! I'm curious about the possibility to insert scripts.
I'm trying to execute an script and I see the script is executed after insertion.
I've two questions:
What about if the inserted script has the flag... async or defer? Those flags will not have effect.
Will you keep this function in the standard? Or are you planning to remove it?
Finally I found (you gave me) the best way to insert content to the document.Thanks a lot!
jakearchibald commentedon Dec 7, 2016
@rianby64 note that the example above creates two paragraphs rather than one.
The scripts will be async, as if you'd created them with
document.createElement('script'). For the streaming solution I mentioned in the OP, I'd like the parser to queue DOM modifications while a non-async/defer script downloads and executes, but allow something like a look-ahead parser.Which function?
createContextualFragment? I don't see why it'd be removed.rianby64 commentedon Dec 7, 2016
OK. Thanks a lot again.
zcorpan commentedon Dec 7, 2016
Indeed.
wanderview commentedon Dec 7, 2016
In general I think we want to be able to provide ReadableStream or Response objects to APIs that currently take a URL. @jakearchibald, would something that let you assign a ReadableStream or Response (backed by a stream) to an iframe.src satisfy your use case?
bzbarsky commentedon Dec 7, 2016
The key part here is to not have to use a separate iframe plus adoption of the current parser insertion point into a different document. Instead, we just want to parse into an existing document location.
wanderview commentedon Dec 7, 2016
This means creating an element that has the concept of partially loaded state, right? An iframe already has all of that, but do other html container elements? So wouldn't we need to create something that has all the load event, error event, and other stateful information of an iframe? Or maybe all that exists today. HTML always catches me out.
jakearchibald commentedon Dec 8, 2016
@wanderview
I think we can get away without this. If an element has a writable endpoint you'll get locking on that writable for free. However, during streaming you'll be able to modify the children of the element, even set the element's
innerHTML. The HTML parser already has to deal with this during page load, so I don't think we need to do anything different.We probably don't need this either.
htmlStream.pipeTo(div.writable)- sincepipeToalready returns a promise you can use that for success/failure info.blaine commentedon Dec 10, 2016
How would this interact / compare with the following scenario:
Rather than fetching HTML snippets from the server, I'm much more likely to be able to fetch [we'll assume newline-delimited to enable stream parsing] a minimal JSON encoding of whatever entity I'm trying to display.
Partially, this is just down to the fact that most web servers wrap HTML output in a series of filters, one of which is a base "<html><head/><body>...</body></html>" template. Obviously, that can [easily, from a purely technical perspective] change, but spitting out independent <div>s is going to take some cultural change on the server side. JSON we have today, and won't take any convincing.
... so, assuming that we use JSON, Is there a performance win to being able to render JSON snippets [as they come over the network] to HTML? The trade-off I'd assume we're making is on triggering additional layouts; put another way, is it faster to do:
while (nextJSONitem) { JSON -> HTML -> DOM }My expectation is that the answer is "it depends"; I don't have a sufficiently reliable playground for testing this to any degree of accuracy, but I would expect we'd want to keep the render pipeline as unobtrusive as possible while minimizing network->screen latency for individual items, using the following as trade-offs:
time to render & re-compute layout for:
total time to:
... ideally all while minimizing client complexity ("they wrote a lot of code to make things that slow"). Thankfully that part should be hidden in frameworks.
... OR am I totally barking up the wrong tree with the idea that JSON is the right delivery mechanism, and we should aim to generate server-side HTML snippets for pretty much anything that can be fetched-with-latency?
16 remaining items
Implement streamAppendHTMLUnsafe
Bug 1998582 [wpt PR 55887] - Implement streamAppendHTMLUnsafe, a=test…
Bug 1998582 [wpt PR 55887] - Implement streamAppendHTMLUnsafe, a=test…
Bug 1998582 [wpt PR 55887] - Implement streamAppendHTMLUnsafe, a=test…
Bug 1998582 [wpt PR 55887] - Implement streamAppendHTMLUnsafe, a=test…
foolip commentedon Nov 27, 2025
In #11964 we had consensus to advance this proposal to stage 2, with the specific scope of adding
streamHTMLUnsafe()and adding therunScriptsoption to bothstreamHTMLUnsafe()andsetHTMLUnsafe().slightlyoff commentedon Feb 4, 2026
Are there any updates on the
stream*()APIs? We have need of them in several products and aren't keen to wait for the declarative patching work.noamr commentedon Feb 4, 2026
Those dont depend on each other. The stream* work is stage 2 and ready to be played with behind a flag in chrome canary. Happy for feedback on that!
b2: v148/renewed-html-insertion-streaming-methods contract + page ids
b2: v148/renewed-html-insertion-streaming-methods contract + page ids
b2: v148/renewed-html-insertion-streaming-methods contract + page ids