
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
push-state-tree
Advanced tools
A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).
A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).
The open-source router system solutions available when I started designing this library were all working with a callback "match" and they are very hard to work with nested levels, and/or not support on-fly changes.
The main goal to have a robust router system is to build Single Page Application, and the in the state of the art is be able to never needs to be reload the page, and be able to add new modules and update the existing ones. To archive it a robust router system is required.
PushStateTree is based in another implementation for a IPTV system for a company called Cianet in Brazil made in 2011, at that time the project was based on Backbone library, and further I decided to create a standalone version compatible with IE8, and what mimic Web Components, to allow (as optional) to expose in the DOM and make easier to debug it.
The following quick start options are available:
bower install pushStateTree
.npm install push-state-tree
.git clone https://github.com/gartz/pushStateTree.git
.Create an PushStateTree
router instance, create the as many instance of rules you need, append to the router, add
listeners and dispatch the router, or do in the order you prefer (always dispatch if you want to events being triggered
in the end of your routine).
This means you can use in the same app rules to navigate using pushState and not using it.
If you chose to don't use the pushState usePushState
option, it will add a hash #
in your pushState urls.
Older browsers will disable it and run as fallback.
Rules are HTML Elements with two special properties rule
and parentGroup
, when you create this HTML elements, you can add it to a PushStateTree instance. When some URL has change it will validate your options and dispatch the right events.
It's possible to create tree of rules, you can bind the result from a parent rule using the parentGroup
option, what will use the match group in the regular expression rule
parent result.
The options passed to rule will be used as element properties, so if you setup id
or className
your element will have this properties.
To create a Rule use the method createRule
from PushStateTree
, then you can appendChild to the instance, example:
var pushStateTree = new PushStateTree();
// Create a Rule element
var myRuleELement = pushStateTree.createRule({
id: 'myFirstRule',
rule: /.+/
});
// Append to pushStateTree to enable
pushStateTree.appendChild(myRuleElement);
If you remove a rule from pushStateTree, it will be disabled and no events will be triggered to it until you append it back.
// Remove from pushStateTree to disable
pushStateTree.removeChild(myRuleElement);
And you can create nested child rules:
// Create a Rule element
var myChildRuleELement = pushStateTree.createRule({
rule: /child/i
parentGroup: 0
});
// Append to myRuleElement to enable
myRuleElement.appendChild(myChildRuleELement);
This child rule depends the myRuleElement
so if the parent dispatch a leave
the child will do it too, even if the rule is matching.
The parentGroup
is the match position from the other rule, example:
var matchExample = 'foo/bar/zaz'.match(/foo\/(.+)\/(.+)/i);
// ---------\/----- this number indicate the parentGroup
matchExample[0]; // foo/bar/zaz
matchExample[1]; // bar
matchExample[2]; // zaz
There is a shortcut to createRule
and appendChild
tha is called add
:
// Create a Rule element
pushStateTree.add({
rule: /foo\/(.+)\/(.+)/i
});
This is the easiest part at all. Rules are HTMLElements so you just need to find then like you do in the DOM.
Using querySelector
:
pushStateTree.querySelector('#id'); // find by id
pushStateTree.querySelector('.class'); // find by class
pushStateTree.querySelector('pushstatetree-rule > pushstatetree-rule'); // find by tag
You also can use querySelectorAll
or any of that stuff. If you are programming with jQuery, you can use it to find, example:
$(pushStateTree).find('myQuery').get(0); // you are accessing the first matched element
You also can add and remove events from this element, the events list are:
event.detail.type
.popstate
or hashchange
are triggered and match with the rule, doesn't matters if is the same url as before.Importante note: match will not dispatch when leave, because it doesn't match.
Options you can pass in the constructor params.
#
in your pushState/replaceState urls.If you chose to support old browsers, make sur your backend detect the browser support to pushState, if it doesn't the feature is auto-disabled, so a good aproach is to redirect the request in the backend to a URL with hash, example: www.exemple.com/my/route/to/app
if browser don't support pushState redirect to www.exemple.com/#my/route/to/app
.
By not doing a redirect in the backend and support non-modern browsers, when the user start navigating using the PushStateTree methods, it will end in adress that looks like this: www.exemple.com/my/route/to/app#my/route/to/anotherApp
You can disable the pushState and use only hash navigation, like we do in our demo, what if you execute by npm start
in your local environment will support pushState support as default, because we use a node server that support it.
Any of the available navigation methods are also available in the all rules, but they will return always the router in the chain.
(state, title, url)
(state, title, url)
popstate
or hashchange
eventpushState(null, null, url).dispatch()
replaceState(null, null, url).dispatch()
Example:
pushStateTree.pushState({foo: 'bar'}, 'no title', 'foo/bar/zaz');
// it changed the url, added to history, but doesn't trigger any event.
pushStateTree.dispatch();
// Now it triggered, and it will check every rule element that match and delegate it events.
pushStateTree.replaceState({foo: 'bar'}, 'no title', 'only/zaz').dispatch();
// The foo/bar/zaz doesn't exists in the browser history anymore, and only/zaz is in it places.
// However it dispatched it popstate event by the way
You can create URL with hash #
it will also work and it will dispatch the event soon it's clicked.
Or you can just change the browser URL, it will dispatch the events.
The properties can be accessed anytime after creating the instance, when it's on DOM some of those will be displayed as a element attribute, that help with the debugging your routes.
You can look the demo page.
git clone https://github.com/cloudhead/node-static.git
npm install --dev
npm start
static/:dynamic/:*nested-group
update
to the route element, dispatch before check rules, allow prevent the change (when prevented, replace the URI by the one before to cancel the navigation).Copyright (c) 2014 Gabriel Reitz Giannattasio Licensed under the MIT license.
Project created by Gabriel Reitz Giannattasio.
This file was generated on Sun Mar 23 2014 00:32:28.
FAQs
A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).
The npm package push-state-tree receives a total of 0 weekly downloads. As such, push-state-tree popularity was classified as not popular.
We found that push-state-tree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.