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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ssr-render-page

v0.3.3

Published

Simple SSR render based on JSDOM

Readme

SSR render page

Simple SSR page rendering tool based on JSDom and node-fetch libraries.

Installation:

npm i ssr-render-page

Usage

const SSRResourceConstructor = require('ssr-render-page')
const configSSRResource = { 
    // Server's origin host name
    origin: 'http://localhost:3000', 
    // Pathname for the root of the SSR page
    resourceName: '/resource/name', 
    // Path to the html file for SSR
    htmlFile: 'path/to/index.html', 
    // Optional: NodeJS' Http server object. Using for live-reloading if development flag is true
    server, 
    // Optional (default = true): Enable live-reloading of your SSR page
    development: true,
    // Optional (default = 8000 ms): Maximum time to wait before stopping page rendering
    waitingTime: 8000 
}
const renderResource = SSRResourceConstructor(configSSRPage)

app.get('/', async (req, res) => {
    const { html, statusCode } = await renderResource('/')
    res.status(statusCode).send(html)
})

To successful resolving of renderResource() promise you have to run window.finishRender() in the browser JavaScript after full rendering of the page. Before that you have to check that window.finishRender() is running on the server side. For this purpose you can check window.SSR flag to establish that this JS file runs in the SSR mode. For example, in the browser JS after finish rendering of the page you have to write this code:

// Browser JavaScript code
if (window.SSR) window.finishRender()

Without the above code SSR rendering doesn't work!

statusCode sets by this code in the browser's JS:

window.statusCode = 404

Very simple SSR example

Project structure:

.
├── client - directory with the browser code
│   ├── index.html
│   └── script.js - browser JS
└── app.js - express server with SSR rendering

app.js - express server with SSR rendering

// app.js - express server with SSR rendering

const path = require('path')
const express = require('express')
const app = express()

const SSRResourceConstructor = require('ssr-render-page')
const resourceName = '/'

/** @type {import("ssr-render-page").renderPage}  */
let renderMessage;

// development config
function passServerToConfig(server) {
    renderMessage = SSRResourceConstructor({ 
        origin: 'http://localhost:3000', 
        resourceName, 
        htmlFile: path.join(__dirname, './client/index.html'),
        server, // Omit if production
        development: true // Omit if production
    })
}

// Page renderer route
app.get(resourceName, async (req, res) => {
    const { html, statusCode } = await renderMessage('/')
    res.status(statusCode).send(html)
})

app.get('/api/message', (req, res) => {
    res.send({ message: 'Hello world' })
})

app.get('*/script.js', (req, res) => {
    res.sendFile(path.join(__dirname, './client/script.js'))
})

const server = app.listen(3000, () => {
    console.log('Server started at http://localhost:3000')
})

passServerToConfig(server)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello page</title>
</head>
<body>
    <div id="root"></div>
    <script src="script.js"></script>
</body>
</html>

script.js - browser JS

// script.js - browser JavaScript

async function start() {
    const root = document.getElementById('root')
    const response = await fetch('/api/message')
    if (response.ok) {
        const { message } = await response.json()
        const header = document.createElement('h1')
        header.textContent = message
        root.append(header)
        // Set Http statusCode to SSR
        window.statusCode = 200
        // Send to SSR that rendering is complete
        window.finishRender()
    } else { 
        root.textContent = 'API error'
        window.statusCode = response.status
        window.finishRender()
        return Promise.reject('API error')
    }
}

// Check that code running in SSR mode (not in the browser)
if (window.SSR) start().catch(console.error)