-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML streaming #11
Comments
I don't think this is really useful, because generators are all synchronous anyway, and concatenating strings is incredibly cheap. I would expect generators to worsen performance. However, supporting promises and streaming the chunk after each one resolves, would boost time to first byte. |
Alright, I will look into that, thanks for the input Perf will definitely degrade I think, here the goal is faster TTFB |
Do you think the TTFB would be slower in the following scenarios? Even though the final result might take longer, the important thing here is that the user will start seeing things much faster, no? app.get("/", function (req, res) {
const htmlContent = htmlGenerator`<html>
<p>${"...your HTML content..."}</p>
</html>`;
const readableStream = Readable.from(htmlContent);
res.send(readableStream);
}); Or (of course will be adapted for Fastify): import { htmlGenerator as html } from "ghtml";
import http from "http";
http.createServer(async (req, res) => {
if (req.url === '/stream') {
res.writeHead(200, { 'Content-Type': 'text/html' });
const htmlContent = html`<html>
<p>${"...your HTML content..."}</p>
</html>`;
for await (const chunk of htmlContent) {
res.write(chunk);
}
res.end();
} else {
res.writeHead(404);
res.end('Not Found');
}
}).listen(3000, () => console.log('Server running on http://localhost:3000')); |
No, TTFB would exactly the same or slower, because all the data is going to the kernel in the same event loop tick. It can be slower because streams adds overhead and you already have all the data. |
Yeah I just watched your masterclass, and to say the least, it came out at the perfect time 😄 So what I had in my mind with htmlGenerator won't improve TTFB, maybe I might add an async generator to combine with readFile, etc, but in any case thanks again for the information and the video 🙏 Closing for now |
Hi! Quick question: I updated the README over at |
It's slightly incorrect. If you have all the data, processing and rendering synchronously will be faster. |
gurgunday/ghtml@c06823f added the
htmlGenerator
function, we can use it to stream templates as they get processedI will probably send a PR in the following days, but just putting it up here so that if anyone is interested, they can give it a shot
The text was updated successfully, but these errors were encountered: