# rlite-router

> A tiny, fast client-side router

Latest version **2.0.3** (published 2017-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install rlite-router
pnpm add rlite-router
yarn add rlite-router
bun add rlite-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2017-08-18 |
| First published | 2015-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 282 |
| Author | Chris Davies |
| Maintainers | chrisdavies |
| Keywords | routing, router |

## Links

- npm: https://www.npmjs.com/package/rlite-router
- Repository: https://github.com/chrisdavies/rlite
- Issues: https://github.com/chrisdavies/rlite/issues
- npm.io page: https://npm.io/package/rlite-router

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 2.0.3 (latest) — 2017-08-18
- 2.0.2 — 2017-08-17
- 2.0.1 — 2017-02-16
- 2.0.0 — 2016-12-22
- 1.1.3 — 2016-01-18
- 1.1.2 — 2015-12-17
- 1.1.1 — 2015-11-03
- 1.1.0 — 2015-08-09
- 1.0.91 — 2015-06-03
- 1.0.82 — 2015-05-29
- 1.0.8 — 2015-05-24
- 1.0.7 — 2015-05-23
- 1.0.6 — 2015-05-23
- 1.0.5 — 2015-05-17
- 1.0.4 — 2015-05-06
- … 6 more at https://npm.io/package/rlite-router/versions

## README

# rlite

Tiny, [fast](http://jsperf.com/rlite/2), light-weight JavaScript routing with zero dependencies.

- Order of route declaration doesn't matter: the most specific route wins
- Zero dependencies
- No performance drop as you add routes
- Less than 700 bytes minified and gzipped
- Parses query strings
- Wildcard support

[![Build Status](https://travis-ci.org/chrisdavies/rlite.svg?branch=master)](https://travis-ci.org/chrisdavies/rlite)

## Usage

Rlite does not come with any explicit tie into HTML5 push state or hash-change events, but these are easy enough to tie in based on your needs. Here's an example:

```javascript
const route = rlite(notFound, {
  // Default route
  '': function () {
    return 'Home';
  },

  // #inbox
  'inbox': function () {
    return 'Inbox';
  },

  // #sent?to=john -> r.params.to will equal 'john'
  'sent': function ({to}) {
    return 'Sent to ' + to;
  },

  // #users/chris -> r.params.name will equal 'chris'
  'users/:name': function ({name}) {
    return 'User ' + name;
  },

  // #users/foo/bar/baz -> r.params.path will equal 'foo/bar/baz'
  'users/*path': function ({path}) {
    return 'Path = ' + path;
  },

  // #logout
  'logout': function () {
    return 'Logout';
  }
});

function notFound() {
  return '<h1>404 Not found :/</h1>';
}

// Hash-based routing
function processHash() {
  const hash = location.hash || '#';

  // Do something useful with the result of the route
  document.body.textContent = route(hash.slice(1));
}

window.addEventListener('hashchange', processHash);
processHash();
```

The previous examples should be relatively self-explantatory. Simple, parameterized routes are supported. Only relative URLs are supported. (So, instead of passing: `'http://example.com/users/1'`, pass `'/users/1'`).

Routes are not case sensitive, so `'Users/:name'` will resolve to `'users/:name'`

One other non-obvious thing is this: if there is a query parameter with the same name as a route parameter, it will override the route parameter. So given the following route definition:

    /users/:name

If you pass the following URL:

    /users/chris?name=joe

The value of params.name will be 'joe', not 'chris'.


## Route handlers

Route handlers ara functions that take three arguments and return a result and/or produce a side-effect.

Here's an example handler:

```javascript
const route = rlite(notFound, {
  'users/:id': function (params, state, url) {
    // Do interesting stuff here...
  }
});
```

The first argument is `params`. It is an object representing the route parameters. So, if you were to
run `route('users/33')`, params would be `{id: '33'}`.

The second argument is `state`. It is an optional value that was passed into the route function. So,
if you were to run `route('users/22', 'Hello')`, params would be `{id: '22'}` and state would be `'Hello'`.

The third argument is `url`. It is the URL which was matched to the route. So, if you were to run
`route('users/25')`, params would be `{id: '22'}`, state would be `undefined` and url would be `'users/25'`.


## Modules

If you're using ES6, import rlite like so:

```javascript
import rlite from 'rlite-router';

const routes = rlite(notFound, {
  '': function () { }
});

// etc
```

Or using [CommonJS](http://www.commonjs.org/) like so:

```javascript
var Rlite = require('rlite-router');
var routes = rlite(notFound, {
  '': function () { }
});

// etc
```


## Handling 404s

The first parameter to rlite is the 404 handler. This function will be invoked when rlite
is called with a URL that has no matching routes.

In the following example, the body will end up with `<h1>404 NOT FOUND</h1>`.

```javascript
const route = rlite(() => '<h1>404 NOT FOUND</h1>', {
  'hello': => '<h1>WORLD</h1>'
});

document.body.innerHTML = route('/not/a/valid/url');
```

## Changes from 1.x

- The parameters to route handlers have changed
- The plugins have been dropped since 2.x is more functional in nature, it's easy to extend
- rlite returns a function, rather than an object
- You can pass optional state into your route handlers
- The result of your route handler is returned by the router

## Installation

Just download rlite.min.js, or use bower:

    bower install rlite

Or use npm: https://www.npmjs.com/package/rlite-router

    npm install --save rlite-router

## Contributing

Make your changes (and add tests), then run the tests:

    npm test

If all is well, build your changes:

    npm run min

This minifies rlite, and tells you the size. It's currently just under 700
bytes, and I'd like to keep it that way!

## Status

Rlite is being actively maintained, but is pretty much feature complete, with the possible exception of support for wildcard routes. Generally, I avoid repos that look stale (no recent activity), but in this case, the reason for inactivity is that library is stable and complete.

## Usage with React

I've been using Rlite along with React and Redux. [Here's a write up on how that works.](https://github.com/chrisdavies/rlite/wiki/Using-with-React)

## License MIT

Copyright (c) 2016 Chris Davies

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/rlite-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
