svelte-guard-history-router
svelte guarded history router
Features
- Named params
- Guards to act when entering / leaving a route
- Standart
<a href="/home">Home</a>
elements
usage
import { Router, route, Guard } from 'svelte-guard-history-router';
import Categories from "./Categories.svelte";
import Category from "./Category.svelte";
import Article from "./Article.svelte";
import Articles from "./Articles.svelte";
import About from "./About.svelte";
import Login from "./Login.svelte";
let session = undefined;
class SessionGuard extends Guard {
async enter(transition) {
if(!session) {
transition.redirect('/login');
}
}
}
export const router = new Router(
[
route("*", Home),
route("/about", About),
route("/login", Login),
route("/article", sessionGuard, Articles),
route("/article/:article", sessionGuard, Article),
route("/category", sessionGuard, Categories),
route("/category/:category", sessionGuard, Category)
],
"/base"
);
<script>
import {
Outlet,
link,
active
} from "svelte-guard-history-router";
import { router } from "./main.mjs";
</script>
<nav>
<a href="/" use:link={router} use:active={router}>Router Example</a>
<ul class="left">
<li>
<a href="/about" use:link={router} use:active={router}>About</a>
</li>
<li>
<a href="/article" use:link={router} use:active={router}>Articles</a>
</li>
<li>
<a href="/category" use:link={router} use:active={router}>Categories</a>
</li>
</ul>
</nav>
<main>
<Outlet {router}/>
</main>
Sample code
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
run tests
export BROWSER=safari|chrome|...
yarn test
or
npm test
API
Table of Contents
Key
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
Properties
RouterState
Type: Object
Properties
Router
key subscriptions:
const aKey = router.keys.aKey;
$aKey
Parameters
routes
Array<Route> (optional, default []
)base
string url (optional, default ""
)
Properties
push
Leave current route and enter route for given path
The work is done by a Transition
Parameters
subscribe
Router subscription
Value changes are fired when the route (or the target component changes)
Parameters
updateActive
Parameters
Transition
Transition between routes
Parameters
Properties
start
start the transition
- find matching target route
- leave old route
- set params
- set current route
- enter new route
end
cleanup transition
redirect
Halt current transition and go to another route.
To proceed with the original route by call continue()
Parameters
path
string new route to enter temprorarly
continue
Continue a redirected route to its original destination
rollback
Bring back the router into the state before the transition has started
Parameters
Route
Base route without guard
Parameters
path
stringcomponent
SvelteComponent target to show
Properties
path
stringcomponent
SvelteComponent target to showpriority
numberkeys
Array<string> as found in the pathregex
RegEx
enter
Enter the route from a former one.
Calls guard enter on all guards present in our gurad but absent in the former one
Parameters
leave
Leave the route to a new one.
Calls guard leave on all our guards which are not in the new route
Parameters
GuardedRoute
Extends Route
Route with a guard
Parameters
path
stringcomponent
SvelteComponent target to showguard
Guard
Properties
enter
Enter the route from a former one.
Calls guard enter on all guards present in our gurad but absent in the former one
Parameters
leave
Leave the route to a new one.
Calls guard leave on all our guards which are not in the new route
Parameters
route
Helper function to create routes with optional guards
Parameters
Guard
Enforces conditions of routes
Like presents of values in the context
attach
Called when guard is attached to a route (one time)
Parameters
enter
Called while entering a route (current outlet is not yet set)
Parameters
leave
Called before leaving a route
Parameters
sequenceGuard
execute guards in a sequence
Parameters
parallelGuard
execute guards in a parallel
Parameters
install
With npm do:
npm install svelte-guard-history-router
With yarn do:
yarn add svelte-guard-history-router
SPA integrating with a nginx backend
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 /deploymantLocation;
try_files $uri$args $uri$args/ /sericeBase/index.html;
}
license
BSD-2-Clause