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

@jeffmcmahan/server-dom

v0.5.0

Published

Simple virtual dom for server-side work

Downloads

1

Readme

Server DOM

Create mutable DOM on the server-side for flexible imperative manipulation.

npm install @jeffmcmahan/server-dom

server-dom makes it easy to do things like this:

const view = dom`<body><header>${primaryNav}</header> ...`

view.querySelector('#home-link').classList.add('active')

Example

Create a virtual DOM fragment as follows:

import {dom} from '@jeffmcmahan/server-dom'

const fragment = dom`<h1 class="title">Hello World!</h1>`

Interact with the DOM using a browser-compatible API:

fragment.querySelector('.title').classList.add('blue')

console.log(fragment.outerHTML) // <h1 class="title blue">He...

Properties & Methods

server-dom's core AstNode class implements a near-equivalent for all relevant DOM Node properties and methods. DOM NodeList and DOMString types are just native javascript equivalents.

Properties

  • childNodes
  • classList
  • className
  • firstChild
  • id
  • isConnected
  • lastChild
  • nextSibling
  • nodeName
  • nodeType
  • ownerDocument (resolves to parent <html> node, if any)
  • parent
  • previousSibling
  • nodeValue

In general, attributes will remain undefined unless and until they are either specified in HTML or added by assignment.

Methods

  • append(node)
  • classList.add(className)
  • classList.contains(className)
  • classList.remove(className)
  • cloneNode([deep])
  • insertBefore(node, referenceNode)
  • querySelector(selector)
  • querySelectorAll(selector)
  • remove()
  • removeChild(node)

† Selector support is currently limited to single tag names, ids, class names, and attributes; no descendants or other combinations (yet).

Additions to Node API:

The DOM Node API does not provide for serialization to HTML, so these items have been added, using HTMLElement naming conventions:

  • innerHTML
  • outerHTML

SVG Support

server-dom doesn't know anything about SVG. It will treat it just like any other set of HTML5 elements, with one simple exception: an empty element within an <svg> must be explicitly closed with a forward slash before the left angle bracket, as shown:

<svg>
	<path d="..." />
</svg>

Why not jsdom or cheerio?

Performance

jsdom is huge and slow; cheerio is advertised to be 8 times faster. With its single-pass parser, server-dom is 5–7 times faster than cheerio.

As a basic benchmark, I took the massive HTML source of the cheerio project page at npm (425kb and ≈11,000 nodes) and after some warmup, I pass the HTML string to cheerio and to server-dom. I don't think statistical treatment is required to appreciate the difference:

| Project | Time | | --- | --- | | cheerio | ≈150ms | | server-dom | ≈25ms |

Dependencies

jsdom installs ≈100 packages and cheerio installs ≈20. Server-dom has zero dependencies.

Requirements

Both cheerio and jsdom are aiming at creating something far more like the real DOM environment than is required for server-side document generation and manipulation for purposes of producing good quality, valid HTML output. Scraping is altogether more demanding, and not something server-dom aims to handle.