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

svelte-hash-router

v1.0.1

Published

Svelte Hash Router

Downloads

59

Readme

svelte-hash-router

npm Build Status GitHub license Dependencies Status

Svelte 3 hash based router. Inspired from svelte-spa-router, but with global routes.

Install

npm i -D svelte-hash-router

Usage

First, set the routes schema before the root component.

// index.js
import { routes } from 'svelte-hash-router'
import App from './App.svelte'
import Home from './Home.svelte'
import About from './About.svelte'

routes.set({
  '/home': Home,
  '/about': About 
})

export default new App({ target: document.body })

Then use Router inside your components.

<!-- App.svelte -->
<script>
import { Router } from 'svelte-hash-router'
</script>

<Router/>

Nested routes

// schema
{
  '/': {
    $$component: MainLayout,
    'home': Home,
    'networking': {
      $$component: Layout,
      '/github': Github,
      '/facebook': Facebook
    }
  },
  '*': NotFound
}

Then just simply use Router for each levels. The parent components won't be re-rendered when switching between children routes.

<!-- MainLayout.svelte -->
<div id='header'></div>

<Router/> <!-- will match '/home' and '/networking' -->
<div id='footer'></div>

<!-- Layout.svelte -->
<p>A social networking</p>
<Router/> <!-- will match '/networking/github' and '/networking/facebook' -->

Each nested level consumes a Router. Once all Router are consumed, the rest will have no effect.

<!-- Layout.svelte -->
<p>A social networking</p>
<Router/> <!-- this will be rendered when the route is active -->
<Router/> <!-- this will not -->
<Router/> <!-- same -->

If $$component in the parent is omitted:

// schema
{
  '/': {
    'home': Home,
    'about': About
  }
}

// will act the same as
{
  '/home': Home,
  '/about': About
}

/ in the first schema will consume the same amount of Router as the second one. The difference is in the first schema, it is an individual route, has its own data and can be looped for children routes when needed. See routes.

Schema

Root paths must start with a / or if using wildcard, *.

import { routes, Router } from 'svelte-hash-router'

route.set({
  '/home': Home,
  '*': NotFound
})

export default new Router({ target: document.body })

An object of options can be passed. All properties starting with $$ will be treated as options, the rest will be seen as nested routes. All options are saved as none-enumerable. $$component is a reserved option.

{
  '/home': Home,
  '/about': {
    $$component: About,
    $$name: 'About me',
    $$customOption: '',
    '/biography': Biography,
    '/hobbies': Hobbies 
  }
}

Params

Get params of current active route with the params store.

// schema
{
  '/books/:id': null,
  '/authors/:name/novels/:title': null
}

// Svelte component
import { params } from 'svelte-hash-router'

// /books/123
$params.id === '123'

// /authors/John/novels/Dreams
$params.name === 'John'
$params.title === 'Dreams'

Same with query.

// Svelte component
import { query } from 'svelte-hash-router'

// /book?id=123&title=Dreams
$query.id === '123'
$query.title === 'Dreams'

Wildcard

The order of schema does matter. Whichever route matching first will be rendered. Wildcard * matches anything, so it is usually put at the end. Wilcard is collected in params as _.

// schema
{ '/book/*': null }

// /book/123?title=Dreams
$params._ === '123' // not catch query

url-pattern

This library uses the nice package url-pattern, check it out for more syntaxes.

Redirect

Redirect routes by using a string instead of a Svelte component, or if passing options object, use $$redirect. The redirect path must be an asbolute path.

{
  '/home': Home,
  '/networking': {
    '/github': Github,
    '*': '/networking/github'
  },
  '*': {
    $$redirect: '/home'
  }
}

The routes store

After the first schema setup, routes becomes readonly. The following reserved properties are added for each route:

  • $$pathname the exact path as in schema define
  • $$href full path including # at the beginning
  • $$stringify generate string from params. Check out url-pattern stringify
  • $$pattern url-pattern object

Since they are non-enumarable, you can easily loop for just nested routes when needed.

<!-- Navigator.svelte -->
<script>
import { routes, active } from 'svelte-hash-router'

$: links = Object.values($routes['/books']['/comedy'])
</script>

{#each links as e}
  <a
    class:active={e === $active}
    href={e.$$href}
    > {e.$$name}
  </a>
{/each}

<style>
.active { color: blue; }
</style>

The store active is current active route. If you use nested routes and want to check if a parent route has an active child route, use the store matches. It is an array including all the parents of the active route and itself.

<script>
import { matches } from 'svelte-hash-router'
</script>

<a class:active={$matches.includes(route)}></a>

A route containing params can be stringified.

<!-- schema: '/book/:id/:name' -->
<a href='{route.$$stringify({id: 123, name: `Dreams`})}'>

<!-- will give: '/book/123/Dreams' -->

CHANGELOG

LICENSE: MIT