svelte guarded history router
- Named params
article/:id
- Guards to act when entering or leaving a route
- Automatic route ranking (more specific routes have higher priority)
- Routes and keys acting as stores
- Nested Routes with relative paths for route composition (Shared Leave Routes)
- Route values
- Object <=> parameter mapping
- Create links from objects
- Standart
<a href="/home">Home</a>
elements
<DetailRoute>
<Route path="/detail/:detail"/>
</DetailRoute>
<Router>
<Route path="/subjectA">
<DetailRoute/>
</Route>
<Route path="/subjectB">
<DetailRoute/>
</Route>
</Router>
<script>
import { Router, Route, Outlet, redirectGuard } from "svelte-guard-history-router";
import About from "./About.svelte";
import Categories from "./Categories.svelte";
import Category from "./Category.svelte";
import Articles from "./Articles.svelte";
import Article from "./Article.svelte";
import { enshureSession } from "./main.mjs";
export const enshureSession = redirectGuard("/login", () => !session);
let session = undefined;
</script>
<Router base="/base">
<nav>
<!-- catch all but link to '/' -->
<Route href="/" path="*" component={Home}>Router Example</Route>
<ul class="left">
<li>
<Route path="/about" component={About}>About</Route>
</li>
<li>
<Route path="/article" guards={enshureSession} component={Articles}>
Articles
<Route path="/:artice" component={Article}/>
</Route>
</li>
<li>
<Route path="/category" guards={enshureSession} component={Categories}>
Categories
<Route path="/:category" component={Category}/>
</Route>
</li>
</ul>
</nav>
<main>
<Outlet/>
</main>
</Router>
Check out the code in the example folder, or the live example.
To run the sample, clone the repository, install the dependencies, and start:
git clone https://github.com/arlac77/svelte-guard-history-router
cd svelte-guard-history-router
npm install
npm start
then navigate to localhost:5000
export BROWSER=safari|chrome|...
yarn test
or
npm test
- active
- Key
- BaseRouter
- BaseTransition
- DetailRoute
- Guard
- redirectGuard
- sequenceGuard
- parallelGuard
- MasterRoute
- nullGuard
- RootRoute
- SkeletonRoute
- ValueStoreRoute
- Transition
- findClosestAttribute
- dispatchNavigationEvent
- nameValueStore
- WaitingGuard
- See: {Router.updateActive}
Keeps the node active state in sync.
node
Elementrouter
Router
Keys also act as svelte stores and can be subscribed.
export const article = derived(
[articles, router.keys.article],
([$articles, $id], set) => {
set($articles.find(a => a.id === $id));
return () => {};
}
);
Type: Object
Extends BaseTransition
key subscriptions:
const aKey = router.keys.aKey;
$aKey // fired if value of aKey changes
routes
Array<Route> all managed routesbase
string url (optional, defaultnew URL("../",import.meta.url).pathname
)
linkNodes
Set<Node> nodes having their active state updatedroutes
Array<Route>keys
Object collected keys of all routesparams
Object value mapping from keys (from current route)route
Route currentnested
Transition ongoing nested transitionbase
string url
Current component. Either from a nested transition or from the current route
Returns SvelteComponent
Value if the current route
Returns any
Returns string url path with fragment & query
Replace current route.
path
string
Replace current route without updating the state.
path
string
Returns Object former state
Leave current route and enter route for given path. The work is done by a Transition.
path
string where to go
Returns Transition running transition
Called from a Transition to manifest the new destination. If path is undefined the Transition has been aborderd.
path
string
Continue a transition to its original destination. Shortcut for this.transition.continue(). If there is no transition ongoing and a fallbackPath is present, it will be entered. Otherwise does nothing.
fallbackPath
string
Abort a transition. Shortcut for this.transition.abort() If there is no transition ongoing and a fallbackPath is present it will be entered. Otherwise does nothing.
fallbackPath
string
Router subscription. Changes in the current route will trigger a update
subscription
Function
Update the active state of a node. A node is considered active if it shared the path prefix with the current route.
node
Element
Add a new Route.
route
Route
Find Route for a given value.
value
any
Returns Route able to support given value
Find path for a given value.
value
anysuffix
string to be appended
Returns string path + suffix
Deliver url search params form the current location.
Returns URLSearchParams as extracted from the path
Replaces the search part of the path with the given searchParams.
searchParams
(URLSearchParams | Object)
Add another transition nesting level. Starts a transition from the given factory.
path
stringfactory
Transition
Continue a nested route to its original destination. Does nothing if the transition has not been nested.
Abort the transition.
error
Returns Promise<boolean> truen in case there was a nesten transition
Extends ValueStoreRoute
Route to represent a slice of the masters list of values.
master
Route route holding the master records
Route holding the list ov values
Returns Route our master
Returns Promise<any> 1st. entry
Returns any last entry
Returns any previous value
Returns Promise<any> next value
Enforces conditions of routes Like the presents of values in the context
Called while entering a route (current outlet is not yet set)
transition
Transition
Called before leaving a route
transition
Transition
Redirects to a given path if condition is met.
Execute guards in a sequence.
children
Iterable<Guard>
Execute guards in a parallel.
children
Iterable<Guard>
Extends SkeletonRoute
Route holding a ordered collection of values.
path
options
value
Array<any>
Default empty guard does nothing.
Route at the root of the tree. This route has no parent. All other routes are below of this one.
Are there parameters in the path.
Returns boolean true if route has parameters (:key)
Returns string empty as we are the root
Returns Object empty object
Returns Guard empty guard which does nothing
Extends RootRoute
Route Subscriptions on Routes fire when the route value changes.
path
options
(optional, default{}
)
path
string full path of the Route including all parentscomponent
SvelteComponent target to showlinkComponent
SvelteComponent content for ObjectLinkpropertyMapping
Object Map properties to object attributes Keys are the property names and values are the keys in the resulting object.priority
numberkeys
Array<string> as found in the pathregex
RegExvalue
any
Enter the route from a former one. All parent routes up to the common ancestor are entered first.
transition
TransitionuntilRoute
Route the common ancestor with the former route
Leave the route to a new one. All parent routes up to the common ancestor are left.
transition
TransitionuntilRoute
Route the common ancestor with the future route
Check if value and properties are acceptable for the route.
value
any to be placed into routeproperties
Object as presented in the route
Returns boolean true if value can be accepted
Extract properties from a value. All property values must be strings.
value
any source of the properties
Returns (Object | undefined) properties extracted from given value
Find common ancestor with an other Route.
other
Route
Returns (Route | undefined) common ancestor Route between receiver and other
Deliver value for a given set of properties of the transition. Default implemantation asks the parent route.
transition
Transition
Returns any for matching properties
Deliver route value. Default implemantation asks the parent route.
Returns any
Deliver property mapping. Default implemantation asks the parent route.
Returns Object for matching properties
Default implemantation asks the parent route.
Extends SkeletonRoute
Route holding a single value
Extends BaseTransition
Transition between routes.
router
Routerpath
string new destination
router
Routerpath
string new destination
Start the transition
- leave old route
- find matching target route @see matcher()
- enter new route
- set params
- set current route
- See: Router.finalizePush
Cleanup transition. Update Nodes active state
Halt current transition and go to another route. To proceed with the original route call continue() The original transition will keept in place and can be continued afterwards.
path
string new route to enter temporary
Bring back the router into the state before the transition has started. All nested transitions also will be termniated.
e
(Exception | undefined)
Walks up the dom tree parents unti a node with a given attribute is found or the root node is reached.
Dispatches a NAVIGATION_EVENT with pathname and hash
event
Event
Create a named object which also acts as a store.
name
stringvalue
any initial value
value
any
Returns Store
Extends Guard
Shows a component during transition.
component
SvelteComponent to show up during th transitionrampUpTime
number initial delay for the componnt to show up (optional, default300
)
With npm do:
npm install svelte-guard-history-router
With yarn do:
yarn add svelte-guard-history-router
All unresolvable requests are redirected to /sericeBase/index.html
- /deploymantLocation is the location of the frontend in the servers file system
- /serviceBase is the url path as seen from the browser
location /serviceBase {
alias /deploymentLocation;
try_files $uri$args $uri$args/ /serviceBase/index.html;
}
publish a _redirects file into the app root folder
/* /index.html 200
BSD-2-Clause