# @ncpa0cpl/vrouter

> Client side router for vanilla js

Latest version **0.0.15** (published 2026-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ncpa0cpl/vrouter
pnpm add @ncpa0cpl/vrouter
yarn add @ncpa0cpl/vrouter
bun add @ncpa0cpl/vrouter
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2026-11-19 |
| First published | 2024-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 88.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Szymon Bretner |
| Maintainers | ncpa0cpl |
| Keywords | router, vanilla |

## Links

- npm: https://www.npmjs.com/package/@ncpa0cpl/vrouter
- Repository: https://github.com/ncpa0cpl/vrouter
- Homepage: https://github.com/ncpa0cpl/vrouter#readme
- Issues: https://github.com/ncpa0cpl/vrouter/issues
- npm.io page: https://npm.io/package/@ncpa0cpl/vrouter

## Dependencies (1)

- [@ncpa0cpl/vanilla-jsx](https://npm.io/package/@ncpa0cpl/vanilla-jsx.md) >=0.0.1-alpha.56

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.0.15 (latest) — 2026-11-19
- 0.0.14 — 2025-05-16
- 0.0.13 — 2025-05-05
- 0.0.12 — 2025-05-03
- 0.0.11 — 2025-05-03
- 0.0.10 — 2025-05-02
- 0.0.9 — 2025-04-30
- 0.0.8 — 2025-04-30
- 0.0.7 — 2025-04-30
- 0.0.6 — 2025-01-02
- 0.0.5 — 2024-11-09
- 0.0.4 — 2024-10-24
- 0.0.3 — 2024-10-15
- 0.0.1 — 2024-10-15

## README

# VRouter

A framework independent router for JavaScript.

## Basic Usage

```ts
import { router } from "@ncpa0cpl/vrouter";
import { renderAboutUsPage, renderHomePage } from "./pages";

// define the routes
const AppRouter = router({
  paramNames: [],
  subroutes: (define) => ({
    home: define({
      paramNames: [],
      title: "Home Page",
      default: true,
      component() {
        return renderHomePage(); // return a HTMLElement
      },
    }),
    about: define({
      paramNames: [],
      title: "About Us",
      component() {
        return renderAboutUsPage(); // return a HTMLElement
      },
    }),
  }),
});

// mount the router on the page
document.body.appendChild(AppRouter.rootElement());

// navigate to the about us page
AppRouter.nav.about.$open();
```

## Query Parameters (slugs)

```ts
// define a route with a arbitrary string in the URL
const AppRouter = router({
  paramNames: [],
  subroutes: (define) => ({
    posts: define({
      paramNames: [],
      subroutes: (define) => ({
        postID: define({
          slug: true,
          component(ctx) {
            const slugValue = ctx.slug.get();
            // listen to slug changes
            ctx.slug.add(() => {
              console.log(ctx.slug.get());
            });

            return renderPostPage();
          },
        }),
      }),
    }),
  }),
});

// navigate to the post page
AppRouter.nav.posts.postID("123").$open();
```

## Search Parameters

```ts
// define a route with search parameters
const AppRouter = router({
  paramNames: [],
  subroutes: (define) => ({
    search: define({
      searchParams: ["q"],
      component(ctx) {
        const searchValue = ctx.params.get().q;
        // listen to search parameter changes
        ctx.params.add(({ q }) => {
          console.log(q);
        });

        return renderSearchPage();
      },
    }),
  }),
});

// navigate to the search page
AppRouter.nav.search.$open({ q: "search query" });
```

## Replace routes and Nested routes

Routes defined in the router can have one of two modes, `replace` or `nest-routes`. A replace mode route will replace its Element with the active child Route if there is one, while a nest-routes mode route will append the active child Route Element to a outlet Element, which can be accessed through the Route context.

```ts
const AppRouter = router({
  paramNames: [],
  mode: "nest-routes",
  component(ctx) {
    const layout = document.createElement("div");
    layout.append(ctx.out()); // append the Outlet Element to the layout div
    return layout;
  },
  subroutes: (define) => ({
    home: define({
      paramNames: [],
      title: "Home Page",
      default: true,
      component() {
        const homeContainer = document.createElement("div");
        // this div will be appended to the layout div created above when this route is opened
        return homeContainer;
      },
    }),
  }),
});
```

---
_Source: https://npm.io/package/@ncpa0cpl/vrouter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
