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

sly-svelte-location-router

v2.2.4

Published

A simple svelte router based on location API

Downloads

50

Readme

sly-svelte-location-router

A lightweight and flexible router for Svelte applications, leveraging the power of the Location API and path-to-regexp for advanced routing capabilities.

Features

  • Simple and intuitive API
  • TypeScript support for enhanced developer experience
  • Lazy-loading of route components for optimized performance
  • Custom loading component support
  • Fallback route handling for 404 pages
  • Robust path matching using path-to-regexp
  • Support for path parameters and query parameters
  • Programmatic navigation
  • Automatic route sorting for optimal matching

Installation

pnpm install sly-svelte-router

Usage

<script lang="ts">
  import { Router } from 'sly-svelte-router';
  import type { Routes } from 'sly-svelte-router';

  const routes: Routes = {
    '/': () => import('./routes/Home.svelte'),
    '/about': () => import('./routes/About.svelte'),
    '/user/:id': () => import('./routes/User.svelte'),
    '/posts/:category/:postId': {
      name: "posts",
      component: () => import('./routes/Post.svelte')
    },
    '/redirect': '/about',
    '*': () => import('./routes/404.svelte'),
  };

  const fallback = () => import('./routes/404.svelte');
</script>

<Router {routes} {fallback}>
  <div>Loading...</div>
</Router>

Route Definitions

sly-svelte-router supports three types of route definitions:

  1. RouteComponent: A function returning a Promise that resolves to a component. Example: () => import('./routes/Home.svelte')

  2. RouteData: An object with 'name' and 'component' properties. Example: { name: "posts", component: () => import('./routes/Post.svelte') }

  3. string: A pathname string for redirection. Example: '/about'

Routes are automatically sorted for optimal matching based on specificity and complexity.

Path Matching

sly-svelte-router uses path-to-regexp for powerful and flexible route matching. This allows for:

  • Named Parameters: /user/:id matches /user/123 and passes {id: '123'} as a parameter.
  • Optional Parameters: /post/:id? matches both /post/123 and /post.
  • Zero or more: /files/* matches any number of segments after /files/.
  • One or more: /files/:path+ requires at least one segment after /files/.
  • Custom matching: Use regular expressions for fine-grained control.

Examples:

  • /user/:id matches /user/123
  • /post/:category/:title? matches /post/tech and /post/tech/new-article
  • /files/:path* matches /files, /files/document.pdf, /files/images/photo.jpg

Route Redirection

You can easily set up route redirections by specifying the target path as a string:

const routes: Routes = {
  '/old-path': '/new-path',
  '/legacy-user/:id': '/user/:id',  // Redirects with parameters
  '/outdated': '/about',
};

API

Router Component

The main component for setting up routing.

Props:

  • routes: An object mapping route paths to component imports or route definitions
  • fallback: A function returning a Promise that imports the fallback component

Navigation

Use the navigate function for programmatic navigation:

import { navigate } from 'sly-svelte-router';

navigate('/user/123', { someState: 'value' });

Stores

Access route information reactively:

import { currentRoute, queryParams, routeState, routeHash, routeParams } from 'sly-svelte-router';

$: console.log($currentRoute); // Current route path
$: console.log($queryParams); // Map of query parameters
$: console.log($routeState); // Current route state
$: console.log($routeHash); // Current route hash
$: console.log($routeParams); // Object containing route parameters

TypeScript Support

sly-svelte-router is written in TypeScript and provides type definitions out of the box for an enhanced development experience.

Performance

Route components are lazy-loaded by default, ensuring that only the necessary code is loaded for each route, optimizing your application's performance.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.