Skip to content

Commit

Permalink
Leverage TextEncoderStream in html response
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed May 18, 2024
1 parent 597b648 commit d931854
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions feat/dependencies-of/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ async function serveHtmlGraphPage(req: Request, modUrl: string, modSlug: string,

// Return the body in two parts, with a comment in between
return new Response(ReadableStream.from((async function*() {
const encoder = new TextEncoder();
yield encoder.encode(pageHtml);

yield encoder.encode("\n<!-- now waiting for graph ... ");
yield pageHtml;

yield "\n<!-- now waiting for graph ... ";
const d0 = Date.now();
const graphText = hideLoadMsg + await graphPromise;
const millis = Date.now() - d0;
yield encoder.encode(`completed in ${millis}ms -->\n\n`);
yield `completed in ${millis}ms -->\n\n`;

yield graphText;

yield encoder.encode(graphText);
}())), {
}())).pipeThrough(new TextEncoderStream()), {
headers: HtmlHeaders,
});
}

0 comments on commit d931854

Please sign in to comment.