thinkjs / thinkjs

Use full ES2015+ features to develop Node.js applications, Support TypeScript.
https://thinkjs.org/
MIT License
5.31k stars 617 forks source link

add stream support for view render? #1245

Closed cbbfcd closed 6 years ago

cbbfcd commented 6 years ago

feature

as the title, there are any plan?

lizheming commented 6 years ago

Offical have no plan for stream render right now. If you have rest time welcome to pull request it! By the way you can read this post https://imququ.com/post/reduce-ttfb-on-thinkjs3-website.html to achieve stream display feature in your project.

cbbfcd commented 6 years ago

Thank you for your suggestion, I will try to achieve it

cbbfcd commented 6 years ago

@lizheming i found a funny UI lib which called markojs

some code like this:

require('marko/node-require');
const { createGzip } = require('zlib');

const Koa = require('koa');
const app = new Koa();

const template = require('./index.marko');

app.use((ctx, next) => {
    ctx.type = 'html';
    ctx.body = template.stream({
        name: 'Frank',
        count: 30,
        colors: ['red', 'green', 'blue']
    });

    ctx.vary('Accept-Encoding');
    if (ctx.acceptsEncodings('gzip')) {
        ctx.set('Content-Encoding', 'gzip');
        ctx.body = ctx.body.pipe(createGzip());
    }
});

app.listen(8080);

Ctx.body itself supports stream, so if I think, if a template engine supports stream output, it is indirectly implementing support for stream。 So what I think is from the think-view, I don’t know if I’m totally wrong.

lizheming commented 6 years ago

think-view just an extend for ThinkJS, we can use thinkjs without think-view absolutely. As your code show, you can use markojs directly in ThinkJS, or you can wrap markojs to an extend named like think-marko.

cbbfcd commented 6 years ago

yep! i will try! thx!