Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@bjornlu/svelte-router
Advanced tools
An easy-to-use SPA router for Svelte.
npm install @bjornlu/svelte-router
hash
and path
based navigationbase
tag navigation// router.js
import { initRouter } from '@bjornlu/svelte-router'
import Home from './Home.svelte'
import Profile from './Profile.svelte'
import ProfileWelcome from './ProfileWelcome.svelte'
import ProfileBio from './ProfileBio.svelte'
import Null from './Null.svelte'
// Initialize the router
initRouter({
// The routing mode: "hash" or "path"
mode: 'history',
// Define routes from the most specific to the least specific
routes: [
{
path: '/',
component: Home
},
{
// Use ":variable" to use named parameters
path: '/profile/:id',
// Component with childrens must have a <slot />
component: Profile,
children: [
{
path: '/welcome',
component: ProfileWelcome
},
{
path: '/bio',
component: ProfileBio
}
]
},
{
path: '/secret',
// Redirect to a new path if this route is matched.
// Also accepts a function that returns a string, and may be asynchronous.
// Learn more in the Recipes section.
redirect: '/'
},
{
path: '/*',
component: Null
}
]
})
<!-- App.svelte -->
<script>
import { RouterView } from '@bjornlu/svelte-router'
</script>
<main>
<RouterView />
</main>
// main.js
import App from './App.svelte'
import './router'
const app = new App({
target: document.getElementById('app')
})
export default app
<Link />
Prop | Type | Default | Description |
---|---|---|---|
to | string | LocationInput | Target route | |
replace | boolean | false | Replace current route instead of pushing |
aria-current="page"
when link exactly matcheshref
by resolving base path, param shorthand paths and hash prepends, e.g. /foo/:id
=> /foo/123
to
and current route path:
link-active
when link partially matches, e.g. /foo
matches /foo/bar
link-exact-active
when link exactly matches, e.g. /foo
matches /foo
navigate
navigate
has two function signatures:
navigate(to: number)
Navigate using an offset in the current history. Works the same way as history.go
.
navigate(to: string | LocationInput, replace?: boolean)
Navigate to a route using a string or an object. string
and LocationInput
are semantically equal and are just different ways to express the same route. For example:
'/foo?key=value#top'
// same as
{
path: '/foo',
search: { key: 'value' },
hash: '#top'
}
Both string
's path and LocationInput
's path can take advantage of the param shorthand syntax to easily define absolute route navigation.
Example usage:
navigate('/foo/bar')
navigate('/foo/bar?key=value')
navigate('?key=value')
navigate('#hey', true)
navigate('#/foo/bar')
navigate({ path: '/foo/bar', search: { key: 'value' } })
navigate({ path: '/foo/:id', hash: '#hey' })
navigate({ search: '?key=value' })
In hash mode,
path
will take precedence overhash
. e.g./foo#/bar
will navigate to/foo
.
route
Svelte Router exports a readable store route
that contains the current route information.
Property | Type | Example | Description |
---|---|---|---|
path | string | '/foo' | |
params | Record | { id: '123' } | The parsed path parameters, e.g. /foo/:id |
search | URLSearchParams | The path search parsed with URLSearchParams | |
hash | string | '#hey' | The path hash with leading # . Empty string if no hash. |
matched | RouteRecord[] | The array of route records for all nested path segments of the current route. The matched records reference the records defined in the routes configuration. |
Check out the recipes section for more advanced use-cases and solutions.
All contributions are welcomed. For any major changes, please open an issue first :)
MIT
0.2.1 - 2020-09-01
<Link />
will work properly with special key clicks, e.g. ctrl-clickFAQs
An easy-to-use SPA router for Svelte
The npm package @bjornlu/svelte-router receives a total of 5 weekly downloads. As such, @bjornlu/svelte-router popularity was classified as not popular.
We found that @bjornlu/svelte-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.