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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-hotwire-middleware

v1.0.2

Published

A lightweight TypeScript middleware for [Express](https://expressjs.com/) to seamlessly handle [Hotwire Turbo](https://turbo.hotwired.dev/) requests. This middleware simplifies detecting Turbo Frame requests and sending Turbo Stream responses.

Readme

Express Hotwire Middleware

A lightweight TypeScript middleware for Express to seamlessly handle Hotwire Turbo requests. This middleware simplifies detecting Turbo Frame requests and sending Turbo Stream responses.

Features

  • Turbo Detection: Automatically detects if a request is from a Turbo Frame or accepts Turbo Streams (req.isTurbo).
  • Frame Identification: Easily access the Turbo Frame ID (req.turboFrame).
  • Stream Responses: Typed helper to send Turbo Stream responses (res.sendTurboStream) with correct Content-Type.
  • TypeScript Support: First-class TypeScript support with extended Express definitions.

Installation

npm install express-hotwire-middleware

Usage

Setup

import express from 'express';
import { turboMiddleware } from 'express-hotwire-middleware';

const app = express();

// Apply the middleware
app.use(turboMiddleware);

Request Handling

Check if the request is a Turbo request or specifically from a Turbo Frame:

app.get('/feed', (req, res) => {
  if (req.isTurboFrame) {
    // The request came from <turbo-frame id="...">
    console.log(`Request from frame: ${req.turboFrame}`);
    res.render('partials/feed');
  } else {
    // Full page load
    res.render('layout', { content: 'feed' });
  }
});

Sending Turbo Streams

Use res.sendTurboStream to send one or multiple stream actions. The Content-Type text/vnd.turbo-stream.html is set automatically.

app.post('/todos', (req, res) => {
  const newTodo = { id: 1, text: 'Buy milk' };

  // Send a single stream action
  res.sendTurboStream({
    action: 'append',
    target: 'todo-list',
    html: `<li id="todo-${newTodo.id}">${newTodo.text}</li>`
  });
});

Batch Updates

You can send multiple actions in a single response:

app.post('/clear-completed', (req, res) => {
  res.sendTurboStream([
    { action: 'remove', target: 'todo-1' },
    { action: 'update', target: 'counts', html: '<span>0 items left</span>' },
    { action: 'replace', target: 'notification', html: '<div class="alert">Cleared!</div>' }
  ]);
});

API Reference

Request Extensions

The middleware extends Express.Request with:

  • req.isTurbo: boolean
    • true if Turbo-Frame header exists OR Accept header contains text/vnd.turbo-stream.html.
  • req.isTurboFrame: boolean
    • true if Turbo-Frame header is present.
  • req.turboFrame: string | undefined
    • The value of the Turbo-Frame header (the ID of the frame).

Response Extensions

The middleware extends Express.Response with:

  • res.sendTurboStream(message: TurboStreamMessage | TurboStreamMessage[])
    • Sends the formatted <turbo-stream> HTML.
    • Sets Content-Type: text/vnd.turbo-stream.html.

Types

TurboStreamAction

Supported actions for Turbo Streams:

  • 'append': Appends the content to the target element.
  • 'prepend': Prepends the content to the target element.
  • 'replace': Replaces the target element with the content.
  • 'update': Updates the content of the target element.
  • 'remove': Removes the target element.
  • 'after': Inserts the content after the target element.
  • 'before': Inserts the content before the target element.

TurboStreamMessage

interface TurboStreamMessage {
  action: TurboStreamAction;
  target: string; // The DOM ID of the target element
  html?: string;  // The HTML content (optional for 'remove' action)
}

License

MIT