Routing setup#
Four steps turn route definitions into routes the compiler checks: a missing provider, a misspelled input or a route pointing at nothing becomes a build error instead of a blank screen. Architecture tests then keep those proofs from quietly disappearing.
Do this once per app, then let the CLI write new routes for you.
This guide assumes an app that consumes @craft-ts/core.
Prefer the guided version
Learn step 9 walks through the same setup on a single route, with the reasoning attached.
Prerequisites#
Install the runtime package and the dev tooling in your app:
npm install @craft-ts/core
npm install -D @craft-ts/dev-tools
1. Add a DI check to every routed component#
DI is checked next to the route it covers. Every routed component must pair its RouteCheckedDI check with CanRun; checks do not cross a loadChildren boundary.
import {
craftRoutes,
type CanRun,
type RouteCheckedDI,
} from '@craft-ts/core';
export const { appRoutes } = craftRoutes('app', [
/* routes */
]);
type _CheckAppDI = RouteCheckedDI<
{
deps: Record<never, never>;
provided: Record<never, never>;
publicProperties: Record<never, never>;
},
never,
CraftRouter,
'app route'
>;
type _CanRunApp = CanRun<_CheckAppDI>;
RouteCheckedDI compares the dependency contract derived from the Craft component with:
the dependencies declared by the routed component
the providers available from the app, parent mount, route and component
If a route depends on a service that is not provided, or if a routed component expects an input that the route does not supply, _CanRunApp turns that mismatch into a TypeScript error in the routes file.
Typical errors look like:
The Counter service is not provided in path: "some-path"Input "userId" is not provided in path: "some-path"
2. Define routes with craftRoute and collect them with craftRoutes#
Collect routes with craftRoutes(...). For Craft components, spread loadCraftComponent(...) into the route: the helper carries the component's dependency contract into the route type, so you do not repeat it as componentDeps.
Breaking rename
The former route(...) helper has been renamed to craftRoute(...). There is no compatibility alias: update both the import and every call site.
import { loadCraftComponent } from '@craft-ts/component';
import { craftRoutes } from '@craft-ts/core';
export const { appRoutes } = craftRoutes('app', [
{
path: 'tasks',
...loadCraftComponent(({ withRetry }) =>
withRetry(import('./tasks/tasks')).then(
({ default: component }) => component,
),
),
},
]);
loadCraftComponent(...) wires the lazy component and its dependency contract together. The per-route RouteCheckedDI / CanRun proof remains in the route file; see the tested example in Learn step 9.
Prefer the route CLI for day-to-day authoring#
The CLI is the primary writing façade while the generated result remains ordinary editable TypeScript:
npx craft route add
npx craft route add /users/:userId --component src/app/users/user-detail.ts#UserDetailComponent
By default it detects the project and craftRoutes collections, creates one lazy routes file per feature, adds the lazy loader, .withParent, the parent mount assertion and the same-file DI check, then runs ESLint and TypeScript diagnostics. Use --dry-run to inspect the plan, --yes for non-interactive scripts and --json for machine-readable output. Route targets must already be Craft components.
Static redirects stay in the selected collection:
npx craft route add /old-users --redirect-to /users --parent src/app/app.routes.ts#appRoutes
Existing flat groups can be split explicitly:
npx craft route split \
--parent src/app/app.routes.ts#appRoutes \
--prefix users \
--target src/app/users/users.routes.ts
The split command only moves statically analyzable routes. It reports local declarations or dynamic paths without mutating files, so business logic is never guessed.
Then wire the crafted routes into your application config:
import { craftAppConfig, provideCraftRouter } from '@craft-ts/core';
import { appRoutes } from './app.routes';
export const appConfig = craftAppConfig({
providers: [provideCraftRouter(appRoutes.toRoutes())],
});
Notes:
appRoutes.toRoutes()gives the router the real runtime routes.Route params are bound to component inputs by name; there is nothing to opt into.
provideCraftRouter(...)also takes the craft loading features (withErrorComponent,withRouteLoadError,withTransitionTimings, …) in the same call, e.g.provideCraftRouter(appRoutes.toRoutes(), withErrorComponent({ component: MyGlobalErrorScreen })).Render
CraftRouterOutlet()from@craft-ts/componentinside your Craft component tree: the URL commits immediately and the outlet drives the pending UI and centralised exception handling. (The features also work standalone viaprovideCraftLoading(...).)withRouteLoadError(...)must stay inprovideCraftRouter(...)because it also registers an a navigation error handler and an internal recovery route. See Non-blocking navigation & pending UI and Route Load Errors.For lazy routes,
loadChildrenshould return the named route tree exported by the child collection, for examplechildRoutes.childRoutes.
When a routes file gets big#
RouteCheckedDI checks one component at a time, so its cost does not grow with the number of sibling routes. Split routes with loadChildren when code splitting or ownership boundaries make that useful — each child still needs its own per-route checks. See Scaling routes.
3. Install the ESLint rules#
Several checks in this guide enforce the same-file DI proof, the exhaustiveness assert and the architecture itself.
Installing the plugin and the rule list is its own page: ESLint rules.
4. Make the DI contract enforceable#
The proofs in this guide are unused type aliases unless they stay in the file: comment out a CanRun and the project still compiles. That is the one fragile step in an otherwise compile-time guarantee.
Architecture tests close it. assertRouteDiProofs walks the static graph and fails unless every routed component — including lazy loadChildren collections — every pending or error screen, and every craftAppConfig error surface is hooked to an armed mapper. TypeScript still judges whether a dependency is provided; the architecture suite judges whether that judgement was invoked.
Copy the demo layout (apps/demo/architecture/) and add:
it('requires a DI proof on every routed component and app-config error screen', () => {
assertRouteDiProofs(graph.graph);
});
Full setup — analysis tsconfig, catalog, Nx target — is on Architecture rules.
See Also#
CLI automation — let the CLI write routes for you
Architecture rules —
assertRouteDiProofskeeps the proofs armedRoute guards — the next thing you'll add
Scaling routes — when one routes file gets too big