Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-guard-history-router

Package Overview
Dependencies
Maintainers
1
Versions
276
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-guard-history-router

svelte router for spa with history only

  • 1.1.20
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
426
decreased by-39.49%
Maintainers
1
Weekly downloads
 
Created
Source

Svelte v3 npm License minified size downloads Build Status semantic-release styled with prettier Commitizen friendly Known Vulnerabilities

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 // fired if value of aKey changes

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
  • node

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
  • e Exception

Route

Base route without guard

Parameters

  • path string
  • component SvelteComponent target to show

Properties

  • path string
  • component SvelteComponent target to show
  • priority number
  • keys Array<string> as found in the path
  • regex 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 string
  • component SvelteComponent target to show
  • guard 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

Keywords

FAQs

Package last updated on 30 May 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc