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:

Route Mapping

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-1 but NOT /blog itself

  • To 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():

  1. Site's setup() method executes

  2. Matched Page's onPrepare() method executes

  3. Matched Components' onPrepare() methods execute

Phase 2: Execution (Asynchronous)

Runs after DOM ready via dispatcher.execRoute():

  1. Site's exec() method executes

  2. Matched Page's onLoad() method executes

  3. Matched Components' onLoad() methods execute

Instance Persistence (Critical Fix)

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:

  1. Exact matches first: /about matches before /about/*

  2. Wildcard matches second: /blog/* matches /blog/post-1

  3. No 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

  1. Create dispatcher once - Store in a variable and reuse for both setup and exec

  2. Use @page decorator - Simplifies route registration

  3. Import all pages - Import page files in routes.ts to trigger decorators

  4. Wildcard for CMS - Use /* for collection template pages

  5. Extend 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