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-micro-router

v1.1.8

Published

Svelte-micro-router does what it says on the tin. It is very micro, please keep this in your mind if you are missing some features.

Downloads

37

Readme

Svelte-micro-router does what it says on the tin. It is very micro, please keep this in your mind if you are missing some features.

Features

  • Small size / Micro Uncompressed: 8147 bytes Gzip: 3223 bytes Brotli: 2875 bytes
  • Easy configuration (no sub route objects)
  • Typescript
  • Support for multiple router slots
  • Async routes for lazy loading modules

Installation

npm install svelte-micro-router --save

Code Sample

This router is composed out of three parts (RouterInstance, RouterSlot, RouterLink). The RouterInstance what has the main logic (route matching, reading parameters from url). RouterSlot is to show the component of the current route.

// router-config.ts
import {RouterInstance, Route} from "svelte-micro-router";
import HomeComponent from "./Components/Home.svelte";
import AboutComponent from "./Components/About.svelte";
import UserComponent from "./Components/User.svelte";

RouterInstance.registerRoutes([
    new Route ('/', HomeComponent),
    new Route ('/about/', AboutComponent),
    new Route('/about-async/', null, () => new Promise(resolve => 
        setTimeout(() => resolve(AboutComponent), 2000))),
    new Route ('/user/:userId/', UserComponent),
    new Route ('/user/:userId/:name/', UserComponent, null, { metaInformation: 'something' }),
]);
// App.svelte
<script lang="ts">
    import {RouterLink, RouterSlot} from "svelte-micro-router";
</script>

<main>
    <nav">
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about/">About</RouterLink>
        <RouterLink to="/about-async/">About-Async</RouterLink>
        <RouterLink to="/user/1/">User 1</RouterLink>
        <RouterLink to="/user/2/test/">User 2</RouterLink>
    </nav>

    <RouterSlot></RouterSlot>
</main>

Usage advices

This router instantiates the route’s component on every route change. Even when routing to a route with the same component as the previous route.

Methods

The router is having the following public methods.

/**
* Register routes to the router.
* @param  {Route[]} routes
* @returns void
*/
public registerRoutes(routes: Route[]): void

/**
* Unregister routes to the router.
* @param  {Route[]} removeRoutes
* @returns void
*/
public unregisterRoutes(removeRoutes: Route[]): void

/**
* To navigate to some other page.
* @param  {string} url
* @param  {boolean=true} pushState
* @returns void
*/
public navigate(url: string, pushState: boolean = true): void

/**
* Returns an object with the params from the url cobmined with meta information.
* @param  {string} url
* @param  {Route} route
* @returns Record
*/
public getCurrentParamsObj(): Record<string, string>
  
/**
* The method addEventListener() sets up a function that will be called whenever the
* specified event is delivered to the target.
* @param  {string} name
* @param  {(args: EventListenerArgs) => void} handler
* @returns void
*/
public addEventListener(name: string, handler: (args: EventListenerArgs) => void): void

/**
* The EventTarget.removeEventListener() method removes from the EventTarget an event
* listener previously registered with EventTarget.addEventListener().
* @param  {string} name
* @param  {(args: EventListenerArgs) => void} handler
* @returns void
*/
public removeEventListener(name: string, handler: (args: EventListenerArgs) => void): void

Events

/**
* Will be called before the url is changed. Change will be cancled if the argument property cancled is set to true.
*/
RouterInstance.addEventListener('url-changing', arg => {
  if (auth.invalid) {
    arg.cancled = true;
  }
});

Contribute

Please contribute with unit tests.

Development

Install the dependencies...

npm install

...then start Rollup:

npm run serve

Navigate to localhost:5000. You should see your app running. Edit a component file in src, save it, and reload the page to see your changes.

By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the sirv commands in package.json to include the option --host 0.0.0.0.

To run unit tests:

npm run test

To run unit tests on every change:

npm run test:watch

Building and running in production mode

To create an optimised version of the app:

npm run build

You can run the newly built app with npm run start. This uses sirv, which is included in your package.json's dependencies so that the app will work when you deploy to platforms like Heroku.