Skip to content

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 ​

ConceptDescription
RouteA mapping between a URL path and a Jadis component (the "page").
Route GroupA collection of routes sharing a common prefix and optional shared layout.
NavigationChanging 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 PointThe DOM element where route components are rendered.

Quick Start ​

typescript
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);

The router supports two modes:

ModeURL FormatUse Case
history (default)/aboutClean URLs; requires server-side fallback
hash/#/aboutSimple deployments, no server config needed
typescript
const router = new Router(routes, { mode: 'history' });

Base URL ​

If your app is hosted in a subdirectory, specify the baseUrl:

typescript
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:

typescript
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:

typescript
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 :param matches across /: /user/:id also 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 with links: 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 a target, a download or rel="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 with rel="external". In hash mode, write href="#/about".
  • Every navigation creates the page component again, and its root component too: state kept in them is lost. A page that declares onRoute is kept while the routes stay on it, and one that declares canLeave can 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.

typescript
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.

typescript
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 ​