npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genz

v0.10.8

Published

Streaming-first SSR for Node Apps

Downloads

28

Readme

Gen Z

Streaming-first SSR for Node Apps.

⚠️ WARNING: This is exprimental.

Contents

Installation

$ npm install genz

Quick Example

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('http://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

Creating HTML

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


Rendering HTML

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.

To a String

Needs documentation...

To a Writable Stream

Needs documentation...

Using Promises

Needs documentation...

Providing a Context

Needs documentation...

Consuming Readable Streams

Needs documentation...


Error Handling

Needs documentation...

Extras

Inline CSS

Needs documentation...

Deduping

Needs documentation...

Deduping experiment

Creating a Custom Tag

If you want to create a tag before you make templates you can do this:


import { createTag } from 'genz';

const myTag = createTag('my-tag'); // myTag({ id: 'nice' }, 'hi') => <my-tag id="nice">hi</my-tag>
const myVoidTag = createTag('my-void-tag', true); // myVoidTag({ id: 'nice' }) => <my-void-tag id="nice">