solid-tiny-router v0.2.5
solid-tiny-router
Tiny routing library for SolidJS
Install
npm i solid-tiny-routeryarn add solid-tiny-routerpnpm add solid-tiny-routerFeatures
- Easy to use: Only 2 components and a 2 utility: 
<Router>,<Link>,useRouterandcreateRouterTree! - Link prefetching: load pages ahead of time with 
router.prefetchand<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.
prefetchallows the given page to be prefetched. Defaults totrue.replacereplaces the current history instead of pushing a new history.scrollscrolls the window to the top of the page after navigation. (Possible values is"auto","smooth"or justundefined, 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>.
pathnameis a reactive property for tracking thewindow.location.pathname.searchis a reactive property for tracking thewindow.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.paramsprovides 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:
scrollscrolls the window to the top of the page after navigation. (Possible values is"auto","smooth"or justundefined, 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