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
A straight-forward and easy-to-use SPA router.
npm install @bjornlu/svelte-router
base
tag navigationBefore mounting your app, initialize the router:
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'
// Initializes the router. Subsequent calls to this function are ignored.
initRouter({
// The routing mode: "hash" or "history". Default: "hash".
mode: 'history',
routes: [
{
path: '/home',
component: Home
},
{
path: '/profile/:id',
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: '/home'
},
{
path: '/*',
component: Null
}
]
})
Make sure routes are defined in the order of the most specific to the least.
Then, in your app add <RouterView />
:
<!-- App.svelte -->
<script>
import { RouterView } from '@bjornlu/svelte-router'
</script>
<main>
<RouterView />
</main>
Done!
Wait. How does
<RouterView />
render nested components? The answer is that it renders them in the component's default<slot />
. So make sure a slot tag is defined for all routes' component with children.
Svelte Router provides 3 ways for navigation. Each with its own use cases:
<Link />
componentProp | 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 prepend, 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
use:link
actionhref
as target routehref
value must start with /
, ?
or #
so that it routesreplace
attribute on anchor tag to replace the route instead of pushingnoroute
attribute on anchor tag to ignore routingWhile similar to the <Link />
component, it does not do a few things:
aria-current="page"
accessibilityhref
resolve, e.g. /foo/:id
will be shown as isIt's recommended to use the <Link />
component wherever possible so that the href
resolves to a valid one. So for example when the user control-clicks the link (open in new tab), it opens a valid route.
navigate
functionnavigate
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
is the 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
.
Svelte Router exports a readable store route
that contains the current route's information.
Property | Type | Example | Description |
---|---|---|---|
path | string | /foo | |
params | Record<string, string> | { 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.1.1 - 2020-08-25
FAQs
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.