Route Dispatcher
The Route Dispatcher is the core routing system in SSE that maps URL paths to Page classes and manages the execution lifecycle.
Overview
The RouteDispatcher handles:
Matching URL paths to Page classes
Managing Site-level code execution
Coordinating the setup/execution lifecycle
Supporting wildcard routes for CMS collections
Basic Usage (v2.0+)
Creating the Dispatcher
In src/routes.ts, create and configure your RouteDispatcher:
import { RouteDispatcher, getAllPages } from "@sygnal/sse-core";
import { Site } from "./site";
// Import pages to trigger @page decorator registration
import "./pages/home";
import "./pages/about";
import "./pages/blog";
export const routeDispatcher = (): RouteDispatcher => {
const dispatcher = new RouteDispatcher(Site);
dispatcher.routes = getAllPages(); // Auto-populated from @page decorators
return dispatcher;
}Using the Dispatcher
In src/index.ts, create the dispatcher once and reuse it:
Critical: The dispatcher must be created once and reused. Creating multiple instances causes data loss between setup and execution phases.
Route Mapping
Automatic Route Discovery (Recommended)
New in v2.0: Use the @page decorator for automatic route registration:
Then import these pages in routes.ts to register them automatically.
Manual Route Registration (Legacy)
You can still manually define routes if needed:
Wildcard Routes
Wildcard paths use a trailing /* to match dynamic segments:
Wildcard Behavior:
/blog/*matches/blog/post-1but NOT/blogitselfTo match both, use two decorators:
Execution Lifecycle
The RouteDispatcher manages a two-phase lifecycle:
Phase 1: Setup (Synchronous)
Runs during <head> load via dispatcher.setupRoute():
Site's
setup()method executesMatched Page's
onPrepare()method executesMatched Components'
onPrepare()methods execute
Phase 2: Execution (Asynchronous)
Runs after DOM ready via dispatcher.execRoute():
Site's
exec()method executesMatched Page's
onLoad()method executesMatched Components'
onLoad()methods execute
Instance Persistence (Critical Fix)
v2.0.0 Critical Fix: RouteDispatcher must be created once and reused to prevent data loss.
Wrong Approach (Data Loss)
This creates two separate RouteDispatcher instances. Any data stored during setupRoute() is lost because execRoute() runs on a different instance.
Correct Approach (Data Preserved)
This ensures the same RouteDispatcher instance is used for both phases, preserving all data.
Route Matching Logic
The dispatcher matches routes in the following order:
Exact matches first:
/aboutmatches before/about/*Wildcard matches second:
/blog/*matches/blog/post-1No match: No page code executes (Site code still runs)
Example:
Accessing Route Information
When using PageBase, route information is automatically available:
Multiple Routes Per Page
Use multiple @page decorators to handle multiple routes with one class:
Best Practices
Create dispatcher once - Store in a variable and reuse for both setup and exec
Use @page decorator - Simplifies route registration
Import all pages - Import page files in routes.ts to trigger decorators
Wildcard for CMS - Use
/*for collection template pagesExtend PageBase - Get automatic context detection and pageInfo access
Example: Complete Setup
Here's a complete example showing proper dispatcher usage:
src/routes.ts:
src/index.ts:
src/pages/blog.ts:
Last updated