Streaming-first SSR for Node Apps.
$ npm install genz
import http from 'http';
import { _, toStream } from 'genz';
http.createServer((req, res) => {
if (req.url !== '/') return res.end();
const content = _.html(
_.head(
_.title('Basic Example'),
_.style(css('body', {
backgroundColor: 'yellow'
}))
),
_.body(
_.h1('Hello World'),
_.p('This is a basic example.')
)
);
res.writeHead(200, {
'Content-Type': 'text/html',
'Transfer-Encoding': 'chunked'
});
toStream(res, content);
}).listen(3000, () => {
console.log('https://localhost:3000');
});
For a more complex example checkout this example.
To explore this example, clone this repo and run:
$ cd path/to/cloned/repo
$ npm i
$ npm run dev
Genz uses conventions familiar to people familiar with the h
function beneath jsx, but with some slight ergonomic changes. so to create content you can do the following:
import { _ } from 'genz';
_.div({ class: 'my-class' },
_.p('Hello there!')
);
...translates to:
<div class="my-class">
<p>
Hello there!
</p>
</div>
Note: Unlike most
h
functions, genz attaches every tag to the_
object (since on the server we don't have to be as precious about package size). This avoids importing tags one by one, which is a pain, and it makes reading the templates a bit easier.
You can pass children as an array, a nested array, as further arguments, or any mixture. So the following all work:
_.div('This is a sentance');
_.div('This ', 'is ', 'a ', 'sentance');
_.div([ 'This ', 'is ', 'a ', 'sentance.' ]);
_.div(['This ', ['is ', 'a ']], 'sentance.');
If you want attributes on a tag you must pass them as the first argument:
_.section({ id: 'my-id', class: 'my-class' }, /* any children... */)
👀 Play with templating in the playground
So far we have only produced a data object that can be sent to a writable stream. Things get a bit more interesting when we render these objects. First, though, let’s take a look at rendering strings.
Needs documentation...
Needs documentation...