# sly-svelte-router

> A spa router for svelte based on the History API

Latest version **1.0.31** (published 2024-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install sly-svelte-router
pnpm add sly-svelte-router
yarn add sly-svelte-router
bun add sly-svelte-router
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.31 |
| Published | 2024-09-08 |
| First published | 2021-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Martin Ottersland |
| Maintainers | slyotis |
| Keywords | svelte, router, spa, history api, slyotis, sly |

## Links

- npm: https://www.npmjs.com/package/sly-svelte-router
- Repository: https://github.com/SlyOtis/svelte-router
- Issues: https://github.com/SlyOtis/svelte-router/issues
- npm.io page: https://npm.io/package/sly-svelte-router

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.31 (latest) — 2024-09-08
- 1.0.30 — 2024-09-08
- 1.0.29 — 2024-09-08
- 1.0.28 — 2021-04-27
- 1.0.27 — 2021-04-27
- 1.0.26 — 2021-04-27
- 1.0.25 — 2021-04-27
- 1.0.24 — 2021-04-27
- 1.0.23 — 2021-04-27
- 1.0.22 — 2021-04-27
- 1.0.21 — 2021-04-27
- 1.0.20 — 2021-04-27
- 1.0.19 — 2021-04-26
- 1.0.18 — 2021-04-26
- 1.0.17 — 2021-04-26
- … 17 more at https://npm.io/package/sly-svelte-router/versions

## README

# sly-svelte-spa-router

A SPA router for Svelte based on the History API.

## How it works

1. Router initialization:
   - The `createRouter` function sets up the router with provided routes and configurations.
   - It adds event listeners for routing changes and popstate events.
   - Initializes the first route based on the current URL.

2. Route changes:
   - Routes can be changed using `goTo`, `goNext`, `goPrev` functions, or `link` and `route` directives.
   - These trigger custom events ('routing-changed' or 'routing-url-changed').

3. Route processing:
   - When a route change is requested, the router checks for restrictions.
   - If restricted, it redirects to an allowed route.

4. Route rendering:
   - The `Router.svelte` component renders the current route component.
   - It also updates the document title and handles slot content.

5. History management:
   - The router uses the History API to manage browser history.
   - It pushes or replaces state depending on the navigation action.

6. Restrictions:
   - Routes can have restrictions that are checked before navigation.
   - Custom restriction resolvers can be provided to handle complex logic.

## Key Components

- `Router.svelte`: Main component for rendering routes.
- `createRouter`: Function to initialize the router.
- `link` and `route` directives: For declarative routing in templates.
- `goTo`, `goNext`, `goPrev`: Programmatic navigation functions.

## DISCONTINUED

This project is no longer maintained. For an updated and improved version, please check out [sly-svelte-location-router](https://www.npmjs.com/package/sly-svelte-location-router).

---
_Source: https://npm.io/package/sly-svelte-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
