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

html-worker

v0.0.2

Published

Build tool for converting HTML with embedded server scripts to Cloudflare Workers

Readme

HTML Worker Builder

A build CLI that converts a single HTML file with embedded server script into a valid Cloudflare Worker structure.

Features

  • 🚀 Convert HTML with <script id="server"> to Cloudflare Worker
  • 📦 Generates complete Worker structure (worker.js, entry.js, wrangler.jsonc)
  • 🔄 Automatic server data injection into HTML via window.serverData
  • 📁 Static file serving for HTML without server script
  • 🎯 Zero dependencies, simple CLI

Usage

Install

npm install html-worker
# or run directly
npx html-worker

Build

npx html-worker input.html [output-dir]
# or if installed locally
node build.js input.html [output-dir]

Example

npx html-worker example.html ./dist
cd dist
npx wrangler dev

Note: You'll need a wrangler.jsonc configuration file in addition to your HTML file. The build process generates the worker files, but Wrangler needs its configuration to deploy and run the worker.

HTML Structure

Your HTML file can include a server script:

<html>
  <head>
    <script id="server">
      export default {
        async fetch(request, env, ctx) {
          // Server logic here
          return new Response(JSON.stringify({ data: "hello" }), {
            headers: { "content-type": "application/json" },
          });
        },
      };
    </script>
  </head>
  <body>
    <div id="app">Loading...</div>
    <script>
      // Access server data via window.serverData
      console.log(window.serverData);
    </script>
  </body>
</html>

Best Practices

Local Development with file://

To enable development by opening HTML files directly in the browser (file:// protocol), always provide fallback dummy data:

<script>
  // Dummy data for file:// development
  const dummyData = {
    title: "Local Development Mode",
    items: ["Item 1", "Item 2", "Item 3"],
    timestamp: new Date().toISOString(),
  };

  // Use server data when available, fallback to dummy data
  const data = window.serverData || dummyData;

  // Your app logic using 'data'
  console.log("Using data:", data);
</script>

This allows you to:

  • Open the HTML file directly in a browser during development
  • Test your frontend logic without running the worker
  • Maintain a consistent data structure between development and production

Server Response Patterns

Preferred: Return JSON at root '/'

For most use cases, return JSON data that gets automatically injected as window.serverData:

<script id="server">
  export default {
    async fetch(request, env, ctx) {
      // Return JSON - gets injected into HTML automatically
      return new Response(
        JSON.stringify({
          title: "Dynamic Title",
          data: await fetchSomeData(),
        }),
        {
          headers: { "content-type": "application/json" },
        }
      );
    },
  };
</script>

Advanced: Custom HTML Templating

If you need server-side templating, you can modify and return ctx.html directly:

<script id="server">
  export default {
    async fetch(request, env, ctx) {
      const data = await fetchSomeData();

      // Modify the HTML directly for templating
      let html = ctx.html.replace("{{TITLE}}", data.title);
      html = html.replace("{{CONTENT}}", data.content);

      return new Response(html, {
        headers: { "content-type": "text/html; charset=utf-8" },
      });
    },
  };
</script>

Recommendation: Use JSON injection for most cases as it provides better separation between server logic and client-side hydration. Only use HTML templating when you need SEO-critical content or complex server-side rendering.

Generated Structure

With Server Script

dist/
├── entry.js          # Main worker entry point
├── worker.js          # Extracted server code
└── wrangler.jsonc     # Worker configuration (required)

Static Only

dist/
├── entry.js          # Basic static handler
├── public/
│   └── index.html    # Static HTML file
└── wrangler.jsonc    # Worker configuration (required)

How It Works

  1. Parse: Extracts <script id="server"> from HTML
  2. Generate: Creates worker.js with server code
  3. Wrap: entry.js handles HTML serving and data injection
  4. Inject: Server JSON data becomes window.serverData in HTML
  5. Deploy: Use wrangler deploy to publish

Server Data Injection

When your server script returns JSON for HTML requests, it gets injected into the HTML as window.serverData, allowing for server-side rendering-like behavior with client-side hydration.

Requirements

  • Node.js (for building)
  • wrangler.jsonc configuration file (for deployment)
  • Cloudflare account (for deployment)