Route Management

Centralized route management system for navigation, breadcrumbs, and page titles.

Overview

All application routes are centralized in $lib/routes.ts. This ensures:

  • Type safety - Routes are typed constants, not magic strings

  • Refactoring safety - Change a route once, updates everywhere

  • IDE support - Autocomplete and "Find References" work correctly

  • Consistency - Single source of truth for all navigation

Rule: Never use hardcoded route strings. Always import from $lib/routes.

Routes Constant

All routes are defined in the Routes object:

import { Routes } from '$lib/routes';

// Static routes
Routes.APP; // '/app'
Routes.APP_SETTINGS; // '/app/settings'
Routes.AUTH_LOGIN; // '/auth/login'
Routes.ADMIN_COMPLIANCE; // '/admin/compliance'

Route Categories

Category
Example Routes
Description

Auth

AUTH_LOGIN, AUTH_LOGOUT

Authentication flows

App

APP_SURVEYS, APP_ALERTS

Main application pages

App Settings

APP_SETTINGS_*

User/org settings

Admin

ADMIN_*

Platform admin pages

Onboarding

ONBOARDING_*

Tenant setup flow

API

API_SET_ACTIVE_TENANT

Internal API endpoints

Using goto()

For client-side navigation:

Using redirect()

For server-side redirects in +page.server.ts or +server.ts:

Using <a> Tags

For declarative navigation in templates:

Building Routes

The buildRoute() function constructs URLs with path segments and query parameters.

Syntax

Examples

Path Encoding

Path segments are automatically URL-encoded:

The breadcrumb system automatically generates navigation breadcrumbs from the current URL.

How It Works

  1. Route Configuration - Each route has metadata (label, detailLabel) in RouteConfig

  2. URL Parsing - buildBreadcrumbs() parses the pathname into segments

  3. Label Resolution - Each segment is matched to its configuration

  4. Dynamic Segments - Unknown segments use the parent's detailLabel

Route Configuration

Generated Breadcrumbs

For URL /app/compliance/frameworks/abc123:

Page Labels

Pages can set custom labels for dynamic breadcrumb segments using the breadcrumb context.

Setting a Page Label

Using the Helper Hook

For simpler cases, use the usePageLabel hook:

Setting a Custom Page Title

Override the auto-generated document title:

Method
Description

setPageLabel(label)

Set custom label for current page's breadcrumb

setCustomTitle(title)

Override the auto-generated page title

reset()

Clear all custom overrides

breadcrumbs

Current breadcrumb array (reactive)

pageTitle

Current page title (reactive)

Page Titles

Page titles are automatically generated from breadcrumbs:

The title is built by:

  1. Taking the breadcrumb labels in reverse order (most specific first)

  2. Appending the app name ("Refresh")

  3. Joining with " | "

Automatic Title Sync

The layout calls useSyncDocumentTitle() to keep document.title in sync:

Adding New Routes

When adding a new route:

1. Add Route Constant

2. Add Route Configuration (for breadcrumbs)

3. Use the Route

Best Practices

Do

Don't

Troubleshooting

If setPageLabel() doesn't update the breadcrumb:

  1. Check context exists - Ensure the layout calls createBreadcrumbContext()

  2. Use $effect - Labels must be set in a reactive context

  3. Check data loading - The label may be set before data loads

Route Not Found

If a route constant doesn't exist:

  1. Add it to Routes in $lib/routes.ts

  2. Add configuration to RouteConfig for breadcrumb support


Last updated: December 2024

Last updated