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

preact-hashish-router

v0.1.13

Published

A simple router for preact

Readme

Preact Hashish Router

A simple router for preact with support for hash and browser routing.

Features

  • browser and hash routing types.
  • Support for lazy-loaded routes (lazy loading).
  • Error handling.
  • Fully typed.
  • Ultra lightweight.
  • Minimal external dependencies.
  • Super fast route matching using unjs/rou3

Installation

npm install preact-hashish-router@latest

Usage

Context Setup

First, ensure your application is wrapped within the router context. This will allow you to access routes and related functions.

import { NotFound, Route, Router, RouterErrorBoundary } from "preact-hashish-router";
import { About } from "./routes/About";
import { AllLevelWildcard } from "./routes/AllLevelWildcard";
import { Home } from "./routes/Home";
import { OneLevelWildcard } from "./routes/OneLevelWildcard";
import { ProductDetails } from "./routes/ProductDetails";

export function App() {
  return (
    <RouterErrorBoundary>
      <Router type="browser">
        <Route
          path="/"
          element={<Home />}
        />
        <Route
          path="/about"
          element={<About />}
        />
        <Route
          path="/product/:id"
          element={<ProductDetails />}
        />
        <Route
          path="/one-level-wildcard/*"
          element={<OneLevelWildcard />}
        />
        <Route
          path="/all-level-wildcard/**"
          element={<AllLevelWildcard />}
        />
        <NotFound element={<h1>Custom Not Found Element</h1>} />
      </Router>
    </RouterErrorBoundary>
  );
}

Using the useRouter Hook

The useRouter hook gives you access to the router context to programmatically navigate or retrieve information about the current route.

import { useRouter } from "preact-hashish-router";

function HomePage() {
  const { go, params, path, searchParams } = useRouter();

  function goToAbout() {
    go("/about");
  }

  return (
    <div>
      <h1>Home Page</h1>
      <button onClick={goToAbout}>Go to About</button>
    </div>
  );
}

<A /> Component for Navigation

import { A } from "preact-hashish-router";

export default function Header() {
  return (
    <header>
      <nav>
        <A href="/">Home</A>
        <A href="/about">About</A>
      </nav>
    </header>
  );
}

DataRouter

Alternative you can use createDataRouter

import { createDataRouter, NotFound, RouterErrorBoundary } from "preact-hashish-router";
import { lazy } from "preact/compat";
import { AllLevelWildcard } from "./routes/AllLevelWildcard";
import { Home } from "./routes/Home";
import { OneLevelWildcard } from "./routes/OneLevelWildcard";
import { ProductDetails } from "./routes/ProductDetails";

const AboutLazy = lazy(() => import("./routes/About"));

const DataRouter = createDataRouter([
  {
    path: "/",
    element: <Home />,
    children: [
      {
        path: "about",
        lazy: true,
        fallback: <h1>Loading About...</h1>,
        element: <AboutLazy />,
        children: [
          {
            path: "test",
            element: <h1>About/test</h1>,
          },
        ],
      },
      {
        path: "product/:id",
        element: <ProductDetails />,
      },
      {
        path: "one-level-wildcard/*",
        element: <OneLevelWildcard />,
      },
      {
        path: "all-level-wildcard/**",
        element: <AllLevelWildcard />,
      },
    ],
  },
]);

export function App() {
  return (
    <RouterErrorBoundary>
      <DataRouter
        onRouteDidChange={(url) => {
          console.log("onRouteDidChange", url);
        }}
        onBeforeRouteChange={(url) => {
          console.log("onBeforeRouteChange", url);
        }}
      >
        <NotFound element={<h1>Custom Not Found Element</h1>} />
      </DataRouter>
    </RouterErrorBoundary>
  );
}

Development

If you have any improvements or find any issues, feel free to contribute or open an issue in the associated repository.