Overview
Jadis includes a built-in, lightweight router for component-based navigation. It is designed to be simple yet powerful enough for single-page applications with dynamic routes, route groups, and shared layouts.
Key Concepts
| Concept | Description |
|---|---|
| Route | A mapping between a URL path and a Jadis component (the "page"). |
| Route Group | A collection of routes sharing a common prefix and optional shared layout. |
| Navigation | Changing the current route: router.goto() by name, router.gotoPath() by path, replace() and replacePath() in place of the history entry, links with links: true. |
| Mount Point | The DOM element where route components are rendered. |
Quick Start
import { Router, defineRoutes } from '@jadis/core';
const routes = defineRoutes({
home: { path: '/', page: HomePage },
about: { path: '/about', page: AboutPage },
});
const router = new Router(routes);
router.mountOn(document.getElementById('app') ?? document.body);Navigation Modes
The router supports two modes:
| Mode | URL Format | Use Case |
|---|---|---|
history (default) | /about | Clean URLs; requires server-side fallback |
hash | /#/about | Simple deployments, no server config needed |
const router = new Router(routes, { mode: 'history' });Base URL
If your app is hosted in a subdirectory, specify the baseUrl:
const router = new Router(routes, { baseUrl: 'my-app' });
// Routes resolve relative to /my-app/Route Parameters
Dynamic segments (:param) are passed as attributes to the page component:
const routes = defineRoutes({
user: { path: '/user/:id', page: UserPage },
});Navigating to /user/42 renders <user-page id="42"></user-page>.
Parameters are encoded in the URL and decoded when it is read: router.goto('user', { id: 'Jürg' }) writes /user/J%C3%BCrg, and the page gets id="Jürg", as router.match('/user/J%C3%BCrg') and a link to that address do. Give goto the value itself, not an encoded one.
Query
The query is not part of matching: /user/42?tab=posts (or /#/user/42?tab=posts in hash mode) matches /user/:id. Read it from router.currentRoute.query, a URLSearchParams, or from the route a routed page receives.
Hash
In history mode, the hash is not part of matching either: /guide/deploying#base matches /guide/deploying, by gotoPath, by match and by a followed link, and stays in the URL. It is the route's hash ('#base', or ''). The router does not scroll to the element it names: see Routed Pages. In hash mode the hash holds the route, and hash is empty.
Root Components
Routes can specify a rootComponentSelector for shared layouts:
const routes = defineRoutes({
home: {
path: '/',
page: HomePage,
options: { rootComponentSelector: 'app-layout' },
},
});This wraps the page component inside the specified layout component, which shows it through a <slot>.
Good to Know
- The first matching route wins, and a
:parammatches across/:/user/:idalso matches/user/42/edit. List specific routes before general ones. - A URL that no route matches throws. End your routes with a catch-all, such as
notFound: { path: '/:rest', page: NotFoundPage }. - Links are followed when asked. In history mode,
<a href="/about">reloads the whole page unless the router is made withlinks: true: it then follows the links to its routes, in shadow roots too, and leaves to the browser a click with a modifier key, a link with atarget, adownloadorrel="external", to another origin, or to a path no route matches. With a catch-all route every path matches: mark the links to files and to the server withrel="external". In hash mode, writehref="#/about". - Every navigation creates the page component again, and its root component too: state kept in them is lost. A page that declares
onRouteis kept while the routes stay on it, and one that declarescanLeavecan hold the person back: see Routed Pages. Keep what must outlive a page in a service or a Bus. - Register the page and root components before navigating to them.
Following Route Changes
router.bus emits change with the route, once its page is shown, the first one included: a frame marks its current place, or writes the tab's title.
router.bus.register('change', (route) => {
document.title = titles[route.name];
}, signal);Showing Pages Your Way
The router replaces the mount point's children with each new page. To keep the page shown until the next one has its data, give mountOn a show function: it puts the next page in the document at once, and removes the current one when it wants.
router.mountOn(outlet, {
show: (next, current) => {
next.hidden = true;
outlet.append(next);
whenLoaded(next).then(() => {
current?.remove();
next.hidden = false;
});
},
});The new page is the router's from then on: it is asked canLeave, and gets onRoute.
See Also
- Declaring Routes — Route definitions and navigation.
- Route Groups — Organizing routes with shared prefixes.
- Routed Pages — Keeping a page between routes, and holding the person back.
- Router API Reference — Complete API documentation.