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

roadtrip

v0.6.2

Published

Client-side routing library. It's about the journey, not just the destination

Downloads

14

Readme

roadtrip

Roadtrip is a client-side web app routing library that understands it's about the journey, not just the destination. If you, like me, need a tiny view-agnostic routing library that handles asynchronous route transitions sensibly, read on.

npm install roadtrip

Getting there is half the fun

With a typical routing library, you register a series of route handlers that are called whenever the user navigates to a new route. But the handler isn't given any information about where the user navigated from. This forces you to jump through some ridiculous hoops.

Suppose you're building a contacts app. When you navigate from the default list view to a specific contact, you want the old screen to slide out to the left while the new screen slides in from the right. When you start editing one of the contact's details, the same thing happens - the old screen slides out to the left, while the editing screen slides in from the right.

What happens when we're done editing, or hit the back button? It makes no sense for the contact screen to slide in from the right again, when a moment ago it slid out to the left. So our /:contact route handler needs to know where we've come from, so it can select the right transition. But most routing libraries treat each route as an isolated moment in time, making that unnecessarily difficult.

By extension, there's no support given for another common use case, in which you need to teardown the current route in an asynchronous fashion before creating the new one. And navigating from /foo to /foo isn't treated as a noop like it should be.

Maybe I've fundamentally misunderstood the problem. If someone can point me to a decent router that solves these issues, let me know so that I can stop working on this one.

Using roadtrip

Don't get too attached to this API, it will almost certainly change.

Basic usage

import roadtrip from 'roadtrip';

roadtrip
  // the home screen of our contacts app
  .add( '/', {
    enter: function ( route, previousRoute ) {
      route.view = displayAllContacts({
      	// if this is the first route we visit (i.e. it's the
      	// page the user lands on), `route.isInitial === true`
      	slideInFrom: route.isInitial ? null : 'left'
      });

      // roadtrip captures scroll position on every navigation,
      // so that you can programmatically scroll to the right
      // part of the page if the user navigates back/forwards
      window.scrollTo( route.scrollX, route.scrollY );
    },

    leave: function ( route, nextRoute ) {
      route.view.teardown({
      	slideOutTo: 'left'
      });
    }
  })

  // individual contact screen
  .add( '/:id', {
    enter: function ( route, previousRoute ) {
      route.view = displayContact({
      	// the `route` object has `params` and `query` properties
      	id: route.params.id,

      	// if the previous route was the home screen, slide in
      	// from the right, otherwise slide in from the left
      	slideInFrom: route.isInitial ? null :
      	  previousRoute.matches( '/' ) : 'right' : 'left';
      });
    },

    update: function ( route ) {
      route.view.set({
        id: route.params.id
      });
    },

    leave: function ( route, nextRoute ) {
      route.view.teardown({
      	slideOutTo: nextRoute.matches( '/' ) : 'right' : 'left'
      });
    }
  })

  // the editing screen
  .add( '/:id/edit', {
    enter: function ( route, previousRoute ) {
      route.view = displayContactEditor({
      	id: route.params.id,
        slideInFrom: route.isInitial ? null : 'right';
      });
    },

    leave: function ( route, nextRoute ) {
      route.view.teardown({
      	slideOutTo: 'right'
      });
    }
  })

  // Calling roadtrip.start() activates the current route
  .start({
    fallback: '/' // if the current URL matches no route, use this one
  });

Asynchronous route transitions

If you need to do some asynchronous work on leaving or entering a route, return a promise from the handler:

function wait ( ms ) {
  return new Promise( function ( fulfil ) {
    setTimeout( fulfil, ms );
  });
}

roadtrip
  .add( '/foo', {
    enter: function ( route ) {
      // if the user clicks a link for `/bar` in the meantime
      // roadtrip will navigate to `/bar` as soon as the promise
      // resolves, unless, in the remaining milliseconds, they
      // decide to navigate to `/baz` instead
      return wait( 1000 );
    },

    leave: function ( route ) {
      // the URL in the address bar changes to that of the new
      // route immediately, but the new route's `enter` handler
      // is not called until this promise resolves
      return wait( 1000 );
    }
  })

  .add( '/bar', ... )
  .add( '/baz', ... )
  .start();

If, on navigating to /bar, you want to kick off some work (e.g. loading the data you're going to need) immediately, regardless of any asynchronous leave handler, you can do that with beforeenter.

roadtrip
  .add( 'foo', ... )

  .add( '/bar', {
    beforeenter: function ( route ) {
      route.data = fetch( '/data.json' )
        .then( function ( response ) { return response.json(); });
    },

    enter: function ( route ) {
      // route.data is a promise, because we set it in
      // `beforeenter`
      return route.data.then( renderView );
    }

    // note: the `leave handler is optional!
  })

  .add( '/baz', ... )
  .start();

If beforeenter returns a promise, the enter handler will only be called once both it and the promise returned from the previous route's leave handler (if any) have resolved.

Navigating programmatically

You can navigate to a new route programmatically:

roadtrip.goto( newUrl );

This will use history.pushState() - if you need to use history.replaceState() instead (e.g. you're redirecting), pass in an option:

roadtrip.goto( newUrl, { replaceState: true });

To redirect to a different route without updating the URL, use the invisible option:

roadtrip.goto( '/page/1', { invisible: true });

Bring your own polyfill

This library assumes your browser supports the HTML5 history API, and Promises. You can polyfill the history API with devote/HTML5-History-API (I haven't tried this, would welcome feedback from anyone who has), and you can provide a Promises/A+ compliant Promise object with roadtrip.Promise === Promise (otherwise it will use window.Promise).

Building and testing

For testing, roadtrip uses jsdom, which requires node.js v4 or later.

To test, run npm test. To build to ./dist, run npm run build (this will fail if the tests fail).

License

MIT.