
Research
/Security News
Popular Tinycolor npm Package Compromised in Supply Chain Attack Affecting 40+ Packages
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
Reactive routing library for universal(isomorphic) web applications. Built with RxJS.
It is meant to be foundation for implementing routing in your application in a way you want it to be.
What makes it different? Why it was created?
It provides:
hashchange
, onbeforeunload
)Above that it:
Library is available on npm as router1
.
When using with React - router1-react would be helpful, it provides all required components.
Also, I tried to make it as un-opinionated as possible, so following parts are not included:
However all this can be quite typical for most of application, and you can see reference implementation here router1-app-template Application template provides webpack build and dev server configurations and routing implementation, also it uses rx-react-container for connecting rxjs logic to react views.
Router consists of following parts
Url pattern for routes can be written using following constructions:
/path/name
- for static path/path/<param>
- for path with parameter/path/<param:\w+>
- for path with parameter that matches regular expression \w+
/path?q
- to add query parameter q
to the urlNote: query parameters can be boolean - if parameter is not passed it would be returned as false
, if there is no string value - as true
createServerHistory
provides static location from url, meant to be used server sidecreateBrowserHistory
provides browser location.
uses html5 history, when available, or location.assign/replace
when history API is not supportedcreateHashLocation
like createBrowserHistory
but using location.hash
to store url, usefull for cordova apps or supporting older browserscreateTestHistory
almost the same as createServerHistory
but allows to navigate, meant to be used for testingRoutes collection is represented by class RouteCollection
, which can be created as new RouteCollection(routes)
Each route is defined as object with following properties:
name
- used to reference specific route (when generating URL, or checking is route active)url
- URL expression for specific routehandler
or handlers
array - "something" that your app will use to handle state associated with the route(more details below)routes
- array of routes, nested in current oneWhen declaring nested routes, they are combined, as following:
name
- combination of all name parts separated by dot symbolurl
- combined URL expressionhandlers
- array with all of the route handlesWhen declaring nested routes, all properties are optional and would be skipped.
For example:
const routeCollection = new RouteCollection([
{
name: 'home',
url: '/home',
handler: homeHandler,
},
{
name: 'info',
url: '/path?query1',
handler: infoHandler,
routes: [
{
handler: defaultHandler,
},
{
name: 'article',
url: '/<articleId:\\d+>',
handler: contactHandler,
},
],
},
]);
Effectively is the same as:
const routeCollection = new RouteCollection([
{
name: 'home',
url: '/home',
handlers: [homeHandler],
},
{
name: 'info',
url: '/path?query1',
handlers: [infoHandler, defaultHandler],
},
{
name: 'info.article',
url: '/path/<articleId:\\d+>?query1',
handlers: [infoHandler, contactHandler],
},
]);
Handlers in route declaration can be anything you like, that would be enough for state handling.
Router has following options:
history
routeCollection
loadState(transition):Observable
loads matched state associated with transition object, if needed data not found and next matching route should be used - should emit falserenderState(state, transition):Observable
- render loaded state, and returns Observable with rendering resultscrollBehavior
object providing onLocationChange
and onHashChange
methods allowing to implement custom scrolling behavior after state was rendered of location hash changedloadState
is called with transition having following properties:
route
- route definition object
name
- route namehandlers
- handlers specified in route configurationparams
- params from matched routelocation
- transition locationforward(url)
- method to trigger redirect to specific locationrouter
- router instance (can be useful to generate redirect or url for example)In case when no matching route was found route
would be set to {name: null, handlers:[]}
)
loadState
should return observable with object to be latter used in renderState
, also to alter navigation behavior it can have following methods:
onHashChange({pathname, search, hash, state})
- method would be called when location hash was changed after rendring (not triggered on first render)onBeforeUnload()
- callback, that would be called before transition from state or user trying to close the page.
Router can be created as following:
const scrollBehavior= new ScrollBehavior(new ScrollManager());
const router = new Router({
history: createBrowserHistory(),
routeCollection,
loadState,
renderState,
// browser scroll behavior; not needed server-side
scrollBehavior,
});
Start/stop listening location changes
start()
- subscribe to location changes, and start handling routes (also called when using renderResult
)stop()
- opposite to start, stop listening to location changesSubscribe to render results:
router.renderResult()
.forEach(renderResult => {
// will be called when route was loaded and rendered
// can be useful for example to track page views
// or if you are have server side app - you can return rendered HTML here
});
To handle onbreforeload
browser event there is callback onBeforeUnload
in router, it can be used as following:
window.onbeforeunload = router.onBeforeUnload;
Other router public methods are:
isActive(route, params)
- check if route is active
createUrl(name, params = {}, hash = '')
- create url for route with paramsnavigate(route, params = {}, hash = '', state = {})
- navigate to route with paramsnavigateToUrl(url, state = {})
- navigate to specific url0.14.2 (2019-02-20)
forceRefresh
is set to trueFAQs
Reactive routing library for universal(isomorphic) web applications
We found that router1 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
/Security News
Malicious update to @ctrl/tinycolor on npm is part of a supply-chain attack hitting 40+ packages across maintainers
Security News
pnpm's new minimumReleaseAge setting delays package updates to prevent supply chain attacks, with other tools like Taze and NCU following suit.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.