import { html, type TemplateResult } from 'lit'; import './pages/home-page.js'; import './pages/about-page.js'; import './pages/not-found-page.js'; /** * Shared route table. * * `@lit-labs/router` is deliberately not used here: it reads `window` and * installs global listeners on connect, so it cannot run during server * rendering. This table is plain data instead, which means the server and the * browser resolve a URL through exactly the same code — the only way to be * confident the two renders agree. */ export interface AppRoute { /** Exact pathname to match. */ readonly path: string; readonly label: string; readonly render: () => TemplateResult; } export const routes: readonly AppRoute[] = [ { path: '/', label: 'Home', render: () => html`` }, { path: '/about', label: 'About', render: () => html`` }, ]; /** * Resolves a pathname to markup, falling back to the 404 page. * * Matching is exact string comparison, which covers a static set of routes. * For parameterised routes swap this for `URLPattern` — it is available in * Node and in current browsers, so it stays isomorphic. */ export function renderRoute(pathname: string): TemplateResult { const route = routes.find((candidate) => candidate.path === pathname); return route ? route.render() : html``; } /** Whether a pathname corresponds to a real route; used for 404 status codes. */ export function routeExists(pathname: string): boolean { return routes.some((candidate) => candidate.path === pathname); }