
Security News
Critical Security Vulnerability in React Server Components
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.
@cerebral/router
Advanced tools
NPM
npm install @cerebral/router
The router of Cerebral does not affect your view layer. A url change triggers a signal that puts your application in the correct state. Your view just reacts to this state, like any other state change.
Read more about the router in the Cerebral in depth - Routing article.
import {Controller} from 'cerebral'
import Router from '@cerebral/router'
const controller = Controller({
modules: {
router: Router({
// Define routes and point to signals
routes: [{
path: '/',
signal: 'app.homeRouted'
}]
// Only react to hash urls
onlyHash: false,
// Set a base url, if your app lives on a subpath
baseUrl: null,
// Will allow none matching routes on same origin to run as normal
allowEscape: false,
// Will make the router not run the initial route
preventAutostart: false
})
}
})
function myAction({router}) {
// If url is "http://localhost:3000/items?foo=bar", returns "http://localhost:3000"
router.getOrigin()
}
function myAction({router}) {
// If url is "http://localhost:3000/items?foo=bar", returns "/items"
router.getPath()
}
Allows you to convert a signal to its corresponding url. This is useful when you actually want to produce the url for a hyperlink etc. To do this you need to create the router in its own file:
router.js
import Router from '@cerebral/router'
export default Router({
routes: [{
path: '/items/:itemKey',
signal: 'items.itemRouted'
}]
})
And attaching it to the controller:
controller.js
import {Controller} from 'cerebral'
import router from './modules/router'
import items from './modules/items'
export default Controller({
modules: {items, router}
})
You will be able to use the same router instance to produce url based on registered signals:
import router from './router'
export default connect({
item: state`items.list.${props`itemKey`}`,
},
function ListItem ({itemKey, item, itemRouted}) {
return (
<li key={itemKey}>
<a href={router.getSignalUrl('items.itemRouted', {itemKey})}>{item.name}</a>
</li>
)
}
)
function myAction({router}) {
// If url is "http://localhost:3000/items?foo=bar", returns "/items?foo=bar"
router.getUrl()
}
function myAction({router}) {
// If url is "http://localhost:3000/items/123?foo=bar", returns "{itemId: '123', foo: 'bar'}"
router.getValues()
}
action
function myAction({router}) {
// Go to a new url
router.goTo('/items')
}
operator
import {goTo} from '@cerebral/router/operators'
export default [
goTo('/items')
]
operator with dynamic URL
import {state, string} from 'cerebral/tags'
import {goTo} from '@cerebral/router/operators'
export default [
goTo(string`/${state`app.currentView`}`)
]
action
function myAction({router}) {
// Go to a new url, replacing current url
router.redirect('/items')
}
operator
import {redirect} from '@cerebral/router/operators'
export default [
redirect('/items')
]
operator with dynamic URL
import {state, string} from 'cerebral/tags'
import {redirect} from '@cerebral/router/operators'
export default [
redirect(string`/${state`app.currentView`}`)
]
action
function myAction({router}) {
// Trigger a signal bound to router
router.redirectToSignal('app.itemsRouted', {foo: 'bar'})
}
operator
import {redirectToSignal} from '@cerebral/router/operators'
export default [
redirectToSignal('app.itemsRouted', props`payload`)
]
import {Controller} from 'cerebral'
import Router from '@cerebral/router'
const controller = Controller({
modules: {
router: Router({
routes: [
{
path: '/',
signal: 'app.homeRouted'
},
{
// Params are passed as props to the signal.
// Query parameters are also passed as props
path: '/projects/:projectId',
signal: 'app.projectRouted',
}
]
})
}
})
When a mapped signal triggers it will trigger with a payload if either params are defined on the route or the url has a query. For example /projects/123?showUser=true will produce the following payload to the signal, available on the props :
{
projectId: '123',
showUser: true
}
function myAction({router}) {
// If url is "http://localhost:3000", changes to "http://localhost:3000/foo"
router.setUrl('/foo')
}
We are currently working on functionality that allows you to bind urls to your state, also allowing you to create more complex relationships between your application state and the url. This API is very likely to change, but please feel free to try it out and give us feedback.
The map property let's you create a mapping between state and
url parameters. This works both ways: when you change the state,
it sets the url from state and when the url changes, it triggers
the state changes.
This automatic mapping is only active if the current url is active. Note also that when you use state mapping, the 'signal' is optional.
import {Controller} from 'cerebral'
import Router from '@cerebral/router'
const controller = Controller({
modules: {
router: Router({
routes: [
{
path: '/projects/:projectId',
map: {projectId: state`app.currentProjectId`},
signal: 'app.projectRouted',
},
{
path: '/settings/:tab',
// whitelist 'focus' query parameter
// and 'tab' url parameter
map: {tab: props`tab`, focus: props`focus`},
signal: 'app.settingsRouted',
}
]
})
}
})
You can use a compute value here to run a computed in order to prepare
the value passed to build the url.
map: {
urlKey: compute(/* ... */)
}
If you use a compute the router cannot map back from the url key to the
state and you need to define a reverse map with rmap:
rmap: {
'some.state': compute(props`urlKey`, (urlKey) => /* ... */),
'other.state': compute(props`urlKey`, (urlKey) => /* ... */)
}
import {Controller} from 'cerebral'
import Router from '@cerebral/router'
import {props, state} from 'cerebral/tags'
const controller = Controller({
modules: {
router: Router({
routes: [
{
path: '/settings/:tab',
// This maps a complex app state to the `opts` param in
// url query.
map: {
opts: compute(
state`projectId`,
state`user.lang`,
(projectId, lang) => ({projectId, lang})
)
},
// This parses the url query `opts` into two state values.
// It does a 'reverse map' hence the 'rmap' name.
rmap: {
'projectId': compute(
state`projectId`,
props`opts`,
(projectId, opts) => opts.projectId || projectId
),
'user.lang': compute(
state`validLangs`,
props`opts`,
(validLangs, opts) => (
validLangs[opts.lang] ? opts.lang : 'en'
)
)
}
}
],
query: true
})
}
})
FAQs
An opinionated URL change handler for Cerebral
The npm package @cerebral/router receives a total of 953 weekly downloads. As such, @cerebral/router popularity was classified as not popular.
We found that @cerebral/router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.

Research
/Security News
We spotted a wave of auto-generated “elf-*” npm packages published every two minutes from new accounts, with simple malware variants and early takedowns underway.

Security News
TypeScript 6.0 will be the last JavaScript-based major release, as the project shifts to the TypeScript 7 native toolchain with major build speedups.