Skip to content
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

Closed
gurgunday opened this issue Apr 6, 2024 · 8 comments · Fixed by #12
Closed

HTML streaming #11

gurgunday opened this issue Apr 6, 2024 · 8 comments · Fixed by #12
Labels
enhancement New feature or request

Comments

@gurgunday
Copy link
Collaborator

gurgunday/ghtml@c06823f added the htmlGenerator function, we can use it to stream templates as they get processed

I 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

@gurgunday gurgunday added the enhancement New feature or request label Apr 6, 2024
@mcollina
Copy link
Owner

mcollina commented Apr 7, 2024

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.

@gurgunday
Copy link
Collaborator Author

gurgunday commented Apr 7, 2024

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

@gurgunday
Copy link
Collaborator Author

gurgunday commented Apr 13, 2024

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'));

@gurgunday
Copy link
Collaborator Author

@mcollina
Copy link
Owner

Do you think the TTFB would be slower in the following scenarios?

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.

@gurgunday
Copy link
Collaborator Author

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

@gurgunday
Copy link
Collaborator Author

Hi! Quick question:

I updated the README over at ghtml to reflect on what I've learned, would you say this explanation is accurate?

@mcollina
Copy link
Owner

It's slightly incorrect. If you have all the data, processing and rendering synchronously will be faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants