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 🙏

© 2024 – Pkg Stats / Ryan Hefner

solid-tiny-router

v0.2.5

Published

Tiny router library for SolidJS

Downloads

8

Readme

solid-tiny-router

Tiny routing library for SolidJS

NPM JavaScript Style GuideOpen in CodeSandbox

Install

npm i solid-tiny-router
yarn add solid-tiny-router
pnpm add solid-tiny-router

Features

  • Easy to use: Only 2 components and a 2 utility: <Router>, <Link>, useRouter and createRouterTree!
  • Link prefetching: load pages ahead of time with router.prefetch and <Link prefetch>.

Usage

import { lazy } from 'solid-js';
import { createRouterTree, Router } from 'solid-tiny-router';

// Declare routes
const routes = createRouterTree([
  {
    path: '/',
    component: lazy(() => import('./pages')),
  },
  {
    path: '/a',
    component: lazy(() => import('./pages/a')),
  },
  {
    path: '/b',
    component: lazy(() => import('./pages/b')),
  },
  // Parametized route
  {
    path: '/parameter/[id]',
    component: lazy(() => import('./pages/[id]')),
  },
  // Wildcard Route
  {
    path: '/wildcard/[...list]',
    component: lazy(() => import('./pages/[...list]')),
  },
]);

const NotFound = lazy(() => import('./pages/404'));

export default function App() {
  return (
    <div>
      <Router
        // Pass routes
        routes={routes}
        // Used for 404
        fallback={<NotFound />}
      />
    </div>
  );
}

// [id].tsx
export default function ParametizedRoute() {
  // Access router
  const router = useRouter();
  // Access parameters
  // For wildcard routes, the params will be an array of string
  const id = () => router.params.id;
  return (
    <div>
      <span>
        {'Welcome to '}
        <span>{`Page ${id()}`}</span>
        !
      </span>
      <div>
        <Link href="/">Go to home</Link>
      </div>
    </div>
  );
}

<Router>

The main routing component. <Router> builds a routing switch from routes and then reactively matches the pages from the window.location. If no matching route is found, fallback rendered, which behaves like a 404 page.

<Link>

Navigation component. Must be used within pages and components controlled by <Router>. <Link> controls the page history and prevents page reload when navigating between local pages.

  • prefetch allows the given page to be prefetched. Defaults to true.
  • replace replaces the current history instead of pushing a new history.
  • scroll scrolls the window to the top of the page after navigation. (Possible values is "auto", "smooth" or just undefined, defaults to "auto".)

useRouter

useRouter provides the router instance for controlling navigation imperatively. This can only be used within pages and components controlled by <Router>.

  • pathname is a reactive property for tracking the window.location.pathname.
  • search is a reactive property for tracking the window.location.search.
  • push(url) pushes a new URL and navigates to the given URL.
  • replace(url) replaces the current history and navigates to the given URL.
  • prefetch(url, isPriority) prefetches the given URL.
  • back() is used to navigate back in history.
  • forward() is used to navigate forward in history.
  • reload() performs page reload.
  • params provides the object based on the parsed URL (if the path of the page is either a wildcard route, a parametized route or a combination of both).

For push, replace, back, and forward, you can pass another parameter opts:

  • scroll scrolls the window to the top of the page after navigation. (Possible values is "auto", "smooth" or just undefined, defaults to "auto".)

createRouterTree

Builds the router tree from an array of Routes. This is used by <Router> to match pages and also to preload component chunks (if lazy was used). Must be called outside of the component and is recommended to be called only once.

SSR

For SSR, you can simply pass a pathname and a search strings to a location object to <Router>

<Router
  location={{
    pathname,
    search,
  }}
/>

License

MIT © lxsmnsyc