Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nanostores/router

nanostores4.3kMIT0.16.1TypeScript support: included

A tiny (712 bytes) router for Nano Stores state manager

nano, router, react, preact, vue, svelte, store

readme

Nano Stores Router

A tiny URL router for Nano Stores state manager.

  • Small. 712 bytes (minified and brotlied). Zero dependencies.
  • Good TypeScript support.
  • Framework agnostic. Can be used with React, Preact, Vue, Svelte, Angular, Solid.js, and vanilla JS.

Since Nano Stores promote moving logic to store, the router is a store, not a component in UI framework like React.

// stores/router.ts
import { createRouter } from '@nanostores/router'

export const $router = createRouter({
  home: '/',
  list: '/posts/:category',
  post: '/posts/:category/:post'
})

Store in active mode listen for <a> clicks on document.body and Back button in browser.

// components/layout.tsx
import { useStore } from '@nanostores/react'

import { $router } from '../stores/router.js'

export const Layout = () => {
  const page = useStore($router)

  if (!page) {
    return <Error404 />
  } else if (page.route === 'home') {
    return <HomePage />
  } else if (page.route === 'list') {
    return <ListPage category={page.params.category} filters={page.search} />
  } else if (page.route === 'post') {
    return <PostPage post={page.params.post} />
  }
}

Made at Evil Martians, product consulting for developer tools.


Docs

Read full docs here.

changelog

Change Log

This project adheres to Semantic Versioning.

0.16.1

  • Fixed types regression.

0.16.0

  • Added hash with #-part to store’s value.

0.15.1

  • Added Nano Stores 0.11 support.

0.15.0

  • Added named capturing group to RegExp routes (by @easing).

0.14.2

  • Fixed :param? RegExp (by @easing).

0.14.1

  • Added Nano Stores 0.10 support.

0.14.0

  • Added search with parsed search params to store’s value.
  • Added search argument to all functions to get and open URL.
  • Removed createSearchParams in favor of route.search of normal router.

0.13

  • Added page object support in getPagePath, openPage, and redirectPage.

0.12.1

  • Fixed parameter output type regression.

0.12

  • Allowed to pass number to router parameter.
  • Removed Node.js 16 support.

0.11

  • Replaced data-no-router to target="_self".
  • Added ignoring clicks with event.preventDefault() calls.

0.10

  • Moved to Nano Stores 0.9.

0.9.1

  • Added funding to package meta.

0.9

  • Moved to Nano Stores 0.8.
  • Moved to TypeScript 5.
  • Removed Node.js 14 support.

0.8.3

  • Fixed URL normalization on search option.

0.8.2

  • Added ParamsArg, RouterConfig, ParamsFromRoutesConfig types export.

0.8.1

  • Fixed optional params in getPagePath() (by Artem Shkurenko).

0.8

  • Added routes params automatic inferring (by Daniil Kozlov).

0.7

  • Moved to Nano Stores 0.6.

0.6

  • Moved to Nano Stores 0.6.

0.5

  • Added opts.links to the stores to disable link clicks tracking.
  • Added defaultPrevented in router click processing.

0.4

  • Removed Node.js 12 support.
  • Added createSearchParams() for ?search store.
  • Added search option to createRouter().
  • Ignored defaultPrevented in click processing.

0.3.1

  • Fixed URL parameters encoding/decoding.

0.3

  • Added optional route parameters (by Eduard Aksamitov).

0.2.2

  • Fixed search params disappear when you click on links (by Andrei Eres).

0.2.1

  • Fixed open() arguments types (by @davidmz).

0.2

  • Moved to Nano Stores 0.5.

0.1.1

  • Fixed Server-Side Rendering support.

0.1

  • Initial release.