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

bun-spa

v1.1.0

Published

Serve single-page apps with Bun, fast and customizable

Readme

bun-spa

Serve bundled SPAs (like a vite build) from a Bun server, fast and simple.

  • What it does: Loads your built files from dist/ (customizable) at startup, caches them in memory, and serves them. Unknown routes fall back to index.html (also customizable).
  • Why it’s fast: Everything is served directly from memory after the initial load. There are no disk reads during requests.
  • Why it’s cool: You can inject dynamic content (like meta tags for social sharing) into your SPA at request time. No need for heavy frameworks like Next.js.

Install

bun add bun-spa

Quick start

import { bunSpa } from "bun-spa";

const app = await bunSpa();

Bun.serve({
  routes: {
    "/*": app
  }
});

bunSpa returns a simple request handler, so it can be passed to the fetch option as well.

Inject dynamic content (optional)

Add a placeholder to your index.html (by default bun-spa looks for <!-- bun-spa-placeholder -->) and provide an indexInjector to replace it at request time. Useful for adding dynamic meta tags for social media previews.

IMPORTANT: If you inject user-provided content, make sure to sanitize it and follow strict guidelines to prevent security issues. See escape-goat, escape-html, sanitize-html, etc.

import { htmlEscape } from "escape-goat";

const app = await bunSpa({
  indexInjector: async ({ url }) =>
    `<meta property="og:description" content="${htmlEscape(
      await fetchDescription(url)
    )}">`
});

Dynamic headers (optional)

Provide a headers callback to set per-request headers. These merge with the default Content-Type the server sets based on the file.

const app = await bunSpa({
  headers: ({ file }) => ({
    "Cache-Control": file.isIndex
      ? "no-store"
      : "public, max-age=31536000, immutable"
  })
});

API

bunSpa(options?: BunSpaOptions): Promise<(req: Request) => Promise<Response>>

BunSpaOptions:

| Option | Type | Default | Description | | -------------------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | | dist | string | "./dist" | Directory scanned at startup; files cached in memory. | | glob | string | "**/*" | Glob pattern for which files to load from dist/. Uses Bun.Glob syntax. | | index | string | "index.html" | SPA entry file served as fallback for unknown routes. | | indexInjectorPlaceholder | string \| RegExp | "<!-- bun-spa-placeholder -->" | Marker in index.html to be replaced at request time. | | indexInjector | (options: BunSpaCallbackOptions) => string \| Promise<string> | undefined | Returns a string that replaces the placeholder in index.html. | | headers | (options: BunSpaCallbackOptions) => Record<string, string> \| Promise<Record<string, string>> | undefined | Additional headers to send with the response. Merged with default Content-Type. | | disabled | boolean | false | If true, the returned handler always responds with disabledResponse. Files aren't loaded. | | disabledResponse | Response | new Response("bun-spa disabled", { status: 501 }) | Response returned when disabled is true. |

Other types:

interface BunSpaCallbackOptions {
  url: URL;
  req: Request;
  file: BunSpaFile;
}

interface BunSpaFile {
  type: string;
  content: ArrayBuffer;
  file: Bun.BunFile;
  isIndex: boolean;
}

Notes

  • Files are read once at startup and kept in memory for fast responses.
  • All unknown paths return index.html (with optional injection).
  • You are responsible for ensuring the security of any dynamically injected content; this library does not perform sanitization.
  • TypeScript types are included.