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
Now with the "server actions" many RPCs could stream HTML directly and use the dom diff to update the real dom changes. I think with morphdom it is feasible because it uses DFS just like how stream chunks arrive, but I have tried to implement it and I don't get away with it.
It would be nice to add documentation on how to use morphdom with streaming.
If it helps this is how I'm getting the nodes from chunks:
constSTART_CHUNK_SELECTOR="S-C";constSTART_CHUNK_COMMENT=`<!--${START_CHUNK_SELECTOR}-->`;constdecoder=newTextDecoder();constparser=newDOMParser();/** * Create a generator that extracts nodes from a stream of HTML. * * This is useful to work with the RPC response stream and * transform the HTML into a stream of nodes to use in the * diffing algorithm. */exportdefaultasyncfunction*parseHTMLStream(streamReader: ReadableStreamDefaultReader<Uint8Array>,ignoreNodeTypes: Set<number>=newSet(),text="",): AsyncGenerator<Node>{const{ done, value }=awaitstreamReader.read();if(done)return;// Append the new chunk to the text with a marker.// This marker is necessary because without it, we// can't know where the new chunk starts and ends.text=`${text.replace(START_CHUNK_COMMENT,"")}${START_CHUNK_COMMENT}${decoder.decode(value)}`;// Find the start chunk nodefunctionstartChunk(){returndocument.createTreeWalker(parser.parseFromString(text,"text/html"),128,/* NodeFilter.SHOW_COMMENT */{acceptNode: (node)=>node.textContent===START_CHUNK_SELECTOR
? 1/* NodeFilter.FILTER_ACCEPT */
: 2/* NodeFilter.FILTER_REJECT */}).nextNode();}// Iterate over the chunk nodesfor(letnode=getNextNode(startChunk());node;node=getNextNode(node)){if(!ignoreNodeTypes.has(node.nodeType))yieldnode;}// Continue reading the streamyield*awaitparseHTMLStream(streamReader,ignoreNodeTypes,text);}/** * Get the next node in the tree. * It uses depth-first search in order to work with the streamed HTML. */exportfunctiongetNextNode(node: Node|null,deeperDone?: Boolean,): Node|null{if(!node)returnnull;if(node.childNodes.length&&!deeperDone)returnnode.firstChild;returnnode.nextSibling??getNextNode(node.parentNode,true);}
Now with the "server actions" many RPCs could stream HTML directly and use the dom diff to update the real dom changes. I think with morphdom it is feasible because it uses DFS just like how stream chunks arrive, but I have tried to implement it and I don't get away with it.
It would be nice to add documentation on how to use morphdom with streaming.
If it helps this is how I'm getting the nodes from chunks:
And this is the code how to use it:
The text was updated successfully, but these errors were encountered: