Home

Awesome

<img src="https://cloud.githubusercontent.com/assets/324440/11302251/2c573b4a-8f94-11e5-9df6-889b19c2ad48.png" width="320" />

Cherrytree is a flexible hierarchical router that translates every URL change into a transition descriptor object and calls your middleware functions that put the application into a desired state.

Installation

The size excluding all deps is ~4.83kB gzipped and the standalone build with all deps is ~7.24kB gzipped.

$ npm install --save cherrytree

In a CJS environment

require('cherrytree')

In an AMD environment, require the standalone UMD build - this version has all of the dependencies bundled

require('cherrytree/standalone')

Docs

Demo

See it in action in this demo.

Plugins

To use cherrytree with React, check out cherrytree-for-react.

Usage

var cherrytree = require('cherrytree')

// create the router
var router = cherrytree()
var handlers = require('./handlers')

// provide your route map
router.map(function (route) {
  route('application', {path: '/', abstract: true}, function () {
    route('feed', {path: ''})
    route('messages')
    route('status', {path: ':user/status/:id'})
    route('profile', {path: ':user'}, function () {
      route('profile.lists')
      route('profile.edit')
    })
  })
})

router.use(function render (transition) {
  transition.routes.forEach(function (route, i) {
    route.view = handlers[route.name]({
      params: transition.params,
      query: transition.query
    })
    var parent = transition.routes[i-1]
    var containerEl = parent ? parent.view.el.querySelector('.outlet') : document.body
    containerEl.appendChild(view.render().el)
  })
})

router.use(function errorHandler (transition) {
  transition.catch(function (err) {
    if (err.type !== 'TransitionCancelled' && err.type !== 'TransitionRedirected') {
      console.error(err.stack)
    }
  })
})

// start listening to URL changes
router.listen()

Examples

You can clone this repo if you want to run the examples locally:

A more complex example in it's own repo:

Features

How does it compare to other routers?

CI

Build Status build status

Browser Support

Sauce Test Status

Cherrytree works in all modern browsers. It requires es5 environment and es6 promises. Use polyfills for those if you have to support older browsers, e.g.:

Acknowledgement

Thanks to Marko Stupić for giving Cherrytree a logo from his http://icon-a-day.com/ project!

FAQ

Want to work on this for your day job?

This project was created by the Engineering team at Qubit. As we use open source libraries, we make our projects public where possible.

We’re currently looking to grow our team, so if you’re a JavaScript engineer and keen on ES2016 React+Redux applications and Node micro services, why not get in touch? Work with like minded engineers in an environment that has fantastic perks, including an annual ski trip, yoga, a competitive foosball league, and copious amounts of yogurt.

Find more details on our Engineering site. Don’t have an up to date CV? Just link us your Github profile! Better yet, send us a pull request that improves this project.