Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Mux | 17,929 | 10,819 | 9,254 | 3 months ago | 23 | December 12, 2021 | 33 | bsd-3-clause | Go | |
A powerful HTTP router and URL matcher for building Go web servers with 🦍 | ||||||||||
Page.js | 7,469 | 1,354 | 217 | a year ago | 48 | April 21, 2020 | 127 | JavaScript | ||
Micro client-side router inspired by the Express router | ||||||||||
Director | 5,517 | 3,434 | 199 | 3 years ago | 29 | February 04, 2015 | 128 | mit | JavaScript | |
a tiny and isomorphic URL router for JavaScript | ||||||||||
Router | 4,614 | 3 | 5 | 4 days ago | 127 | January 31, 2022 | 20 | mit | TypeScript | |
🤖 Type-safe router w/ built-in caching & URL state management for JS/TS, React, Preact, Solid, Vue, Svelte and Angular | ||||||||||
Meatier | 3,115 | 5 years ago | 36 | JavaScript | ||||||
:hamburger: like meteor, but meatier :hamburger: | ||||||||||
Skipper | 2,843 | 2 | 3 | 18 hours ago | 1,004 | September 23, 2022 | 221 | other | Go | |
An HTTP router and reverse proxy for service composition, including use cases like Kubernetes Ingress | ||||||||||
Klein.php | 2,630 | 362 | 65 | 7 months ago | 9 | February 01, 2017 | 97 | mit | PHP | |
A fast & flexible router | ||||||||||
Next Routes | 2,439 | 686 | 78 | 2 months ago | 46 | May 21, 2018 | mit | JavaScript | ||
Universal dynamic routes for Next.js | ||||||||||
Navigo | 2,334 | 215 | 43 | 2 years ago | 161 | April 23, 2021 | 9 | mit | TypeScript | |
A simple vanilla JavaScript router. | ||||||||||
Router5 | 1,707 | 126 | 79 | a year ago | 148 | March 19, 2020 | 94 | mit | TypeScript | |
Flexible and powerful universal routing solution |
XState Router. Add routes to your XState machine and maintain it in sync with the actual route.
Install the library with npm i xstate-router
.
If you don't have XState installed, install it: npm i xstate
Try the live example here: https://codesandbox.io/s/rllly3pyxp.
The routerMachine
function returns an interpreter:
import { routerMachine } from 'xstate-router'
const machineConfig = {
initial: 'main',
context: { myValue: 0 },
states: {
main: { meta: { path: '/' } },
blog: { meta: { path: '/blog' } },
},
}
const service = routerMachine({
config: machineConfig,
options,
initialContext,
})
// The state changes on a route change and the route changes on a state change.
service.onTransition(state => console.log(state.value))
// The context is enhanced with router properties.
service.onChange(ctx => console.log(ctx))
/* Context
{
myValue: 0,
// Router properties:
match,
location,
history,
}
*/
import { useRouterMachine } from 'xstate-router'
const config = {
initial: 'home',
states: {
home: { meta: { path: '/' }, on: { NEXT: 'about' } },
about: { meta: { path: '/about' }, on: { NEXT: 'dashboard' } },
dashboard: {
meta: { path: '/dashboard' },
initial: 'login',
on: { NEXT: 'home' },
states: {
loggedIn: {
initial: 'main',
states: {
main: { meta: { path: '/dashboard/main' } },
data: { meta: { path: '/dashboard/data' } }
}
},
login: {
meta: { path: '/dashboard/login' },
on: { LoggedIn: 'loggedIn' }
}
}
}
}
}
function App() {
const service = useRouterMachine({ config })
return <div>{service.state.value}</div>
}
null
(not matching), {}
(no parameters), { param1: 4711 }
history.location
routerMachine(...)
accepts a history object as fourth parameter. If it is missing it defaults to createBrowserHistory()
(from package 'history'
) and is published in the context.if you translate to a state having a parameterized route then you have to ensure that context.match contains the values of those parameters. Otherwise the placeholder is shown in the route. Example:
states: {
list: { meta: { path: '/items' },
on: {
ShowDetails: {
target: 'details',
actions: assign((ctx, event) => ({
...ctx,
match: { id: event.item }
}))
}
}
}
details: { meta: { path: '/items/:id/details'} }
}
where the event trigger could look like this:
<button onClick={() => this.send('ShowDetails', { item: 817 })}>Show details...</button>
Paths could have parameters such as /items/:id/details
and regular expressions, for more information please read this: pillarjs/path-to-regexp.
If a route changes then a parameterized event 'route-changed'
is fired: e.g. { dueToStateTransition: "true", route: "/blog", service: /* the xstate interpreter */ }
.
dueToStateTransition
is true
. If the route changes because the location was changed (either by the user in the browsers location bar or by a script changing history.location
), then dueToStateTransition
is false
.route
gives you the current route which causes the eventservice
provides the xstate interpreter which can be used to send another event.Placing an on: 'router-changed'
event at a state can be used to avoid leaving the current state if the route changes. Think of a state which might show unsaved data and you want to ask the user 'Leave and loose unsaved data?'. If you decide to accept the new route anyway you have to resend the event:
on: {
'route-changed': {
cond: (context, event) => event.dueToStateTransition === false
&& !event.processed, // interfere only new events
actions: (context, event) => {
if (context.unsavedData) return; // suppress current route change
event.processed = true; // mark event as processed
event.service.send(event); // resend the event to establish the origin route change
}
}
},