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

router4svelte

v1.0.10

Published

client side routing for svelte

Downloads

10

Readme

router4svelte

summary

a client side routing library for svelte projects.

  • uses hash-based routing
  • works with browser buttons
  • redirects standard paths to hash based equivalent
- /posts/hello-world
+ /#/posts/hello-world

install

npm i router4svelte

usage

Initialize

<!-- App.svelte -->
<script lang="ts">
  import { Router, type RouterConfig} from "router4svelte";
  let config:RouterConfig = {};
</script>

<Router {config} />

Navigation

With components

  • You can drop the included Link-component directly in place of anchor-tags.
<script>
  import { Link } from "router4svelte";
</script>

<a    href="/about" class="btn btn-outline-primary">About</a> 

<Link href="/about" class="btn btn-outline-primary">About</Link>

With api

  • You can use the RouterApi to navigate however you want in your code.
<script>
  import { RouterApi } from "router4svelte";
  
</script>

<button on:click={ () => RouterApi.back() }>⬅</button>
<button on:click={ () => RouterApi.routeTo("/about") }>About</button>

configure

The config object maps the paths to one of the following:

  • SvelteComponent
  • RouteOptions-object
  • Function returning RouteOptions-object

a simple config might look like this

  const config = {
    "/": Home,
    "/about": About,
    "/posts/:id": Post,
    DEFAULT: ErrorPage
  }

routes

The routes (keys) of the object are specified as strings, starting with / and may declare route parameters prefixed with :

Examples: / /about /posts/:category /posts/:id/edit/:version

Additionally, a DEFAULT entry can be specified as a fallback, which will be applied if no other routes match. The DEFAULT-entry path is declared in uppercase letters DEFAULT without preceeding /. (See example above and on the bottom of the page)

If multiple entries match the requested path, the last entry will take precedence and a warning will be logged in the console.

route parameters

A route parameter is prefixed with : in the definition, and will by default map to a prop on the target component. Consider this route config:

"/posts/:category/:id": MyPostComponent

A request to /posts/news/32 would produce the equivalent of the following output:

<MyPostComponent category="news" id="32" />

There are no hard limits on route parameters, but keep in mind that if too many parameters are present, they may interfere with other declared routes.

advanced config

you can configure your routes with a RouteOptions-object for more control. see supported properties below. Map your route directly to such an object, or declare a function that returns RouteOptions and optionally takes RequestParams. You can mix and match mappings directly to components, to objects or functions as you please.

type RouteOptions = {
  component: typeof SvelteComponent, // component to render on this route (Required)
  slot?: typeof SvelteComponent, // component to render in the default slot of the main component
  props?: Record<string, any> // props to pass to the rendered components (will merge with and be overriden by conflicting route params)
}

type RequestParams = {
  path: string, // Pathname
  params: Record<string, string>, // route parameters (/:id/) mapped to an object 
  query: Record<string, string> // query parameters (?id=123) mapped to an object
}

const config: RouteConfig = {
  "/posts/:category": {
    component: ListLayout,
    slot: ListOfPosts
  },
  "/posts/:date": ({params}) => {
    try {
      const date = new Date(params.date)
      return { component: ListOfPosts, date }
    } catch(e) {
      return { 
        component: ErrorComponent, 
        props: { error: "Invalid date format" }
      }
    }
  }
}

example App.svelte

<!-- App.svelte -->
<script lang="ts">
  import Nav from "./components/Nav.svelte";
  import Sidebar from "./components/Sidebar.svelte";
  import Products from "./components/Products.svelte"
  import LayoutComponent from "./components/LayoutComponent.svelte";
  import Home from "./pages/Home.svelte";
  import Error  from "./pages/Error.svelte";
  import Posts from "./pages/Posts.svelte";
  import { Router, type RouterConfig } from "router4svelte";

  const config: RouterConfig = {
    "/": Home,
    "/posts/:id": Posts,
    "/:layoutMode/products": {
      component: LayoutComponent,
      slot: Products
    },
    DEFAULT({path}) {
      return {
        component: Error,
        props: { message: `404 - The requested resource ${path} was not found` },
      };
    },
  };
</script>

<Nav />
<div class="container-fluid">
  <div class="row">
    <div class="col">
      <Sidebar />
    </div>
    <div class="col">
      <Router {config} />
    </div>
  </div>
</div>