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-routed

v1.1.2

Published

Svelte routing

Downloads

3

Readme

Svelte Routed

This is a personal solution for routing in svelte used in my projects.

Demo template

Clone demo template:

  1. npx degit andrusenn/RouterSvelteTemplate/template template
  2. cd template
  3. npm install
  4. npm run dev or npm run start

routes.js

Create a file to config Router and all routes.

// routes.js

// Import the components
import Home from "./views/Home.svelte";
import About from "./views/About.svelte";
import NotFound from "./views/NotFound.svelte";

// Vars
let routes = {
    paths: [
        // Paths
        {
            path: "/", // required
            name: "home", // required
            component: Home, // required
            title: "Home",
        },
        {
            path: "/about", // required
            name: "about", // required
            do: function (data) {
                // Do something
                console.log(data);
            },
            // Support dynamic parameters
            // if it's defined, return valid route -> /about/param1
            params: {
                param1: "",
                // if a second param is defined, return valid route -> /about/param1/param2
                param2: "",
            },
            // Add some meta data
            meta: {
                someparam: "foo",
            },
            component: About, // required
            title: "About",
        },
        {
            // Fallback not found
            path: "*",
            component: NotFound,
            title: "404",
        },
    ],
    // some configs an callbacks
    fns: {
        // Base url path
        basePath: "",

        // On update route
        update: (component) => {
            //
        },

        init: () => {
            //
        },

        // middleware / before routing
        before: (next) => {
            //let to = setTimeout(() => {
            next();
            //clearTimeout(to);
            //}, 500);
        },

        // After routing
        after: () => {
            //
        },
    },
};
// export
export default routes;

| name | value | | -------: | ------------------------------------------------------------------------------------------------------------- | | path | [required] is the path show in address bar | | name | [required] name of the route | | do | function callback do:function(data){} | | params | Dynamic parameters. Capture each part of defined parameter. params:{param1:'',param2:''} param1/param2/etc| | meta| Custom data{foo:bar} | | title| page title` |

| configs | Description | | ------------------: | -------------------------------------------- | | basePath | Base root path | | init() | First load | | update(component) | Callback on update component | | before() | Middleware function -> next() must be used | | after() | Callback after routed |

App.svelte

<script>
    // Import components
    import { RouterLink, RouterView } from "svelte-routed";
    // Import my routes
    import myroutes from "./routes.js";
</script>

<main>
    <nav>
        <RouterLink name="home">Home</RouterLink>
        <RouterLink name="about">About</RouterLink>
    </nav>
    <hr />
    <RouterView use="{myroutes}" />
</main>

Availables props

| Attr | Desc | | ---------- | -------------------------------------------------------------------- | | name | The name of the route. The same one declared on routes.js | | part | If you use name, use part to pass search and hash ?x=0#hashvalue | | path | Ypu can use path instead of name | | title | The title attr of the "a" tag | | cssClass | Add classes to "a" tag | | cssStyle | Add styles to "a" tag |

<!-- Use path -->
<RouterLink
    path="/about?x=0#myhash"
    title="mytitle"
    cssClass="classes"
    cssStyle="styles"
    >About</RouterLink
>
<!-- Use name and use part for search and hash-->
<RouterLink
    name="about"
    part="?x=0#myhash"
    title="mytitle"
    cssClass="classes"
    cssStyle="styles"
    >About</RouterLink
>

Routing the components

Routed components can fetch RouterLink and Router.

Example:

Home.svelte (routed)

<script>
    // fetch components
    export let RouterLink, Router;

    // Access to Router
    console.log(Router.params, Router.meta, Router);
</script>

<div>
    <RouterLink name="about" part="?x=0#myhash">About</RouterLink>
</div>

Programmatically route

Router.navigateTo('/path')

// AnyRoutedViewComponent.svelte
<script>
    export let Router;
    Router.navigateTo("/about");
</script>

IMPORTANT: If you are outside of a routed component, use store $Router importing Router;

<script>
    // Import from global store
    import { Router } from "svelte-routed";
    // OnMount
    import { onMount } from "svelte";

    let somecondition = true;

    onMount(() => {
        // If some condition
        if (somecondition) {
            // Use $Router
            $Router.navigateTo("/my_path");
        }
    });
</script>

<div>Some content</div>

Apache server SPA

For those who want to deploy on apache server add this to the .htaccess file.


# In subfolder ------------

<IfModule mod_rewrite.c>
# replace delete basepath with yours.
# Take care of path slashes

RewriteEngine On

# remove trail slash
RewriteRule ^(.*)/$ basePath/$1 [L,R=301]

RewriteBase /basepath/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /basePath/index.html [L]

</IfModule>

# In root -----------------

<IfModule mod_rewrite.c>
RewriteEngine On

# remove trail slash
RewriteRule ^(.*)/$ $1 [L,R=301]

RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

</IfModule>