@daucus/router
Router for Daucus Web Apps
@daucus/router
provides 3 differents solutions to help you use the routes generated by the Daucus CLI (routes.js
):
- a simple
daucus-router
Web Component - a programmatic router
- route matching functions, for custom routing33
Usage Examples
- basic: Snowpack Template
- custom (i18n): fullweb.dev
Programmatic API
The DaucusRouter
class provides a default implementation of the @modern-helpers/router
AbstractRouter
for Daucus.
import { DaucusRouter } from "@daucus/router";
// routes generated by the Daucus CLI
import daucusRoutes from "./_daucus_/routes.js";
const appRouter = new DaucusRouter();
appRouter.run(daucusRoutes, "/the-default-path/", "/base/directory");
For more advanced use cases, use the routeFinder
and i18nRouteFinder
functions to match a path with a Daucus route.
import { routeFinder } from "@daucus/router";
// routes generated by the Daucus CLI
import daucusRoutes from "./_daucus_/routes.js";
const daucusRouteFinder = routeFinder(daucusRoutes);
// custom routing
function myCustomRouter() {
//...
if (path.startsWith("/docs/")) {
const daucusRoute = daucusRouteFinder(path);
//...
}
}
For now, building a custom router is mandatory when using the i18n option with the Daucus CLI.
import { i18nRouteFinder } from "@daucus/router";
// routes generated by the Daucus CLI (with "i18n": true)
import daucusRoutes from "./_daucus_/routes.js";
const daucusRouteFinder = i18nRouteFinder(daucusRoutes);
// custom routing
function myCustomRouter() {
//...
if (path.startsWith("/docs/")) {
const daucusRoute = daucusRouteFinder(path, lang);
//...
}
}
Web Components
daucus-router
Routing wrapper element for Daucus Routes
Wrap the programmatic DaucusRouter for declarative usage.
Properties
Property | Attribute | Type | Default | Description |
---|---|---|---|---|
baseDir |
base-dir |
string |
"templates/" | Directory where templates are published, relative to the baseHRef (should end with a '/') |
defaultPath |
default-path |
string |
"/docs/" | Path of the default route |
routes |
`SimpleRoutesConfig | undefined` |
Events
Event | Description |
---|---|
navigation-end |
A navigation ended without error |
navigation-start |
New navigation begins |
project-change |
A navigation led to a route from another Daucus Project |
route-match |
A corresponding route was found for a navigation |
route-redirection |
A navigation led to a redirection to another route |