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

v2.2.1

Published

Why not another option for routing in svelte?

Downloads

222

Readme

svelte-page

Nested routing with page.js for svelte

How to use

  1. Build a object with all your routes:
export default {
  '/': home,
  '/other': other,
  '/things': {
    component: things,
    data: { foo: 'A custom property' },
  },
  '/things/:thing*': thing,
  '/things/:thing/subthing/': subthing,
}
  1. Pass it to the <Router />
<Router routes={routes} />

<script>
  import routes from './routes.js'

  export default {
    data() {
      return {
        routes
      }
    }
  }
</script>

Every page may receive the following properties:

  • Any property passed to the page programatically with the Router.go(path, { ...props }) or in the routes.js definition
  • params - An object representing all the parameters from the URL
  • page
    • child - The nested route component that should be rendered;
    • props - The props that should be passed to the nested component
  1. For nested routes, use the <NestedRoute>
Here is my sub-page:
<br>
<NestedRoute {page} />

<script>
  export default {
    components: {
      NestedRoute: 'svelte-page/NestedRoute.html'
    }
  }
</script>

<NestedRoute /> can fire events to allow communication between parent and child pages.

Available events:

  • action
  • response
  • event

Example:

<NestedRoute on:action="handle(event)" />

Props

<Router
  routes={routes}
  strict={true}
  hashbang={true}
>
  • routes - The routes object (default: undefined)
  • strict - If false, match trailling slash (default: true)
  • hashbang - Add #! before urls (default: true)

Dynamic:

  • context - The page.js context object
  • path - The current router path

Slots

Both <Router> and <NestedRoute> have the optional default slot which is only rendered when the current route isn't associated with any component.

<Router ...>
  404 - Route not found
</Router>

<NestedRoute>
  Nested route not found
</NestedRoute>

Events

Component events

notFound

When a route isn't found, both the <Router> and <NestedRoute> fire a notFound event.

<Router on:notFound="console.log('Route not found!!!')" />
<NestedRoute on:notFound="console.log('NestedRoute not found!!!')" />

change

When a route change occurs, the <Router/> component fires a change event:

<Router on:change="console.log('Route changed!!!')" />

This event is also fired at the root component with the name of router:change:

export default {
  oncreate() {
    this.root.on('router:change', context => {
      console.log('New path:', context.path)
    })
  },
}

The <Router /> adds itself to the root component as a router property, so it's also possible to get the context by observing its lifecycle:

<!-- Some other component down the hierarchy -->
<script>
  export default {
    oncreate() {
      this.root.router.on('update', ({ current }) => {
        console.log('Current path:', current.context.path)
      })
    }
  }
</script>

Static Methods

<!-- Some other component down the hierarchy -->
<script>
  import Router from 'svelte-page'

  export default {
    oncreate() {
      /** Show the specified route with an optional data object */
      Router.go('/about', optionalData)
      Router.push('/about', optionalData)

      /** Go back to the previous route */
      Router.back()
    }
  }
</script>

Example

App.html

<Router {routes} />

<script>
  import Home from './routes/Home.html';
  import About from './routes/About.html';
  import AboutPersonTemplate from './routes/AboutPersonTemplate.html';
  import AboutPerson from './routes/AboutPerson.html';

  export default {
    components: {
      Router: 'svelte-page/Router.html',
    },
    data() {
      return {
        routes: {
          "/": Home,
          /** About page will have a {name} prop */
          "/about": {
            component: About,
            data: { name: 'Text name' }
          },
          /**
           * Page will have {page.child} because of nested routes
           **/
          "/about/*": AboutPersonTemplate,
          /** Page will have a {params.whoami} prop */
          "/about/:whoami": AboutPerson
        }
      }
    }
  }
</script>

AboutPersonTemplate.html

<NestedRoute {page} />

<script>
  export default {
    components: {
      NestedRoute: 'svelte-page/NestedRoute.html',
    },
  }
</script>

For more examples, please check the example/src/routes.js file.

Credits and inspirations