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

cw-router

v1.0.1

Published

A virtual router for Preact

Readme

cw-router

A minimal, production-ready virtual router for Preact, designed specifically for embedded widgets that use a query parameter as a virtual URL.

Bundle Size License

Why cw-router?

Most routers (like React Router or Wouter) are designed for full-page applications where window.location.pathname is the source of truth. cw-router is different:

  • Virtual URL: Uses the _cw query parameter (e.g., ?_cw=/status/123) instead of the pathname.
  • Embedded Friendly: Perfect for widgets embedded on third-party sites where you can't control the URL path.
  • Ultra Lightweight: Under 2KB (approx. 1.96 KB minified).
  • Preact Native: Built for Preact with zero external dependencies.

Installation

pnpm add cw-router
# or
npm install cw-router

Core Concept: The _cw Contract

Routing is driven exclusively by the _cw query parameter. The router automatically normalizes and matches these values:

| _cw value | Resolved Virtual Route | | --------------- | ------------------------------ | | absent or empty | / (home) | | /status/123 | /status/:id | | /status?c=123 | /status/:id (legacy support) | | /password | /password | | unknown | User-defined fallback |


Usage Example

import { Route, Switch, Link, useLocation } from "cw-router";

const App = () => {
  return (
    <div>
      <nav>
        <Link href="/">Home</Link>
        <Link href="/password">Change Password</Link>
      </nav>

      <Switch>
        <Route path="/" component={Home} />
        <Route path="/status/:id" component={Status} />
        <Route path="/password" component={Password} />
        {/* Fallback for unknown routes */}
        <Route component={NotFound} />
      </Switch>
    </div>
  );
};

const Status = ({ params }) => {
  return <div>Order ID: {params.id}</div>;
};

API Reference

useLocation()

A hook that returns the current virtual path and a navigation function.

const [location, navigate] = useLocation();

// Navigate to a new route
navigate("/status/456");

// Replace the current state
navigate("/home", { replace: true });

<Route />

Renders content if the path matches.

  • path?: string: The pattern to match (e.g., /user/:id).
  • component?: Component: Component to render (receives params prop).
  • children?: Alternative way to render content.

<Switch />

Iterates through children and renders the first matching <Route />. Useful for "Not Found" logic.

<Link />

A wrapper for <a> that prevents full page reloads and updates the virtual location.

  • href: string: The target virtual path.
  • replace?: boolean: Use replaceState instead of pushState.

Features

History Integration

cw-router patches window.history.pushState and window.history.replaceState to ensure that any programmatic navigation updates your components instantly, even if done outside of the React/Preact lifecycle.

Parameter Support

Supports dynamic segments like :id. These are passed as a params object to your route components.


License

ISC