alepha@docs:~/docs/.content/release/framework/docs/reference/primitives$
cat $page.md | pretty
5 min read

#$page

#Import

typescript
1import { $page } from "alepha/react/router";

#Overview

Main primitive for defining a React route in the application.

The $page primitive is the core building block for creating type-safe, SSR-enabled React routes. It provides a declarative way to define pages with powerful features:

Routing & Navigation

  • URL pattern matching with parameters (e.g., /users/:id)
  • Nested routing with parent-child relationships
  • Type-safe URL parameter and query string validation
  • A page REMOUNTS when its params change (/users/1 to /users/2), and the layouts above it keep their state; a query-only change or router.invalidate() re-runs the loader without remounting. State that must survive a param change belongs in the parent layout.

Data Loading

  • Server-side data fetching with the loader function
  • Automatic serialization and hydration for SSR
  • Access to request context, URL params, and parent data

Component Loading

  • Direct component rendering or lazy loading for code splitting
  • Client-only rendering when browser APIs are needed
  • Automatic fallback handling during hydration

Performance Optimization

  • Static generation for pre-rendered pages at build time
  • Automatic page cache: a page that reads nothing about its visitor is cached for everyone, signed in or not (see the cache option)
  • Code splitting through lazy component loading

Error Handling

  • Custom error handlers with support for redirects
  • Hierarchical error handling (child → parent)
  • HTTP status code handling (404, 401, etc.)

Extending a page from another module

  • page.override({ ... }) replaces options of a page you did not write (its component, lazy, loader, head), before the app starts: a shallow merge, so every option you leave out stays as declared
  • parent on your page puts it under a page of another router, and children on your layout adopts another router's pages
  • See the guide "Extending a Module"

#Options

Option Type Required Description
name TName No Identifier name for the page
path string No Add a pathname to the page
schema TConfig No Add an input schema to define: - params: parameters from the pathname
use Middleware[] No Middleware to apply to the loader function
loader Object No Load data before rendering the page
prefetch boolean No Declare the loader free of side effects, so a prefetch may run it
props Object No Default props to pass to the component when rendering the page
component FC<TProps & TPropsParent> No The component to render when the page is loaded
lazy Object No Lazy load the component when the page is loaded
loading ComponentType No Shown in this page's slot when a navigation to it takes longer than 200 ms (reactPageOptions.loadingDelay), with the layouts above it staying in place
overlay PageOverlay<TProps & TPropsParent> No Open this page as an overlay over another one, on an in-app navigation from it: one URL, two presentations
children Array<PagePrimitive> | (() => Array<PagePrimitive>) No Attach child pages to create nested routes, adopting them as children of this page
parent PagePrimitive<PageConfigSchema, TPropsParent, any> No Define a parent page for nested routing
can Object No UI-affordance predicate for this page's navigation entry - NOT security
nav PageNav No Navigation metadata - declares this page's presence in navigation surfaces: the sidebar, the breadcrumb trail, and a command palette (Spotlight)
errorHandler ErrorHandler No Catch any error from the loader function or during rendering
cache false | PageCacheOptions No The automatic page cache, for this page
ssr boolean No Enable or disable server-side rendering for this page
stream boolean No Buffer the HTML instead of streaming it, so the page can choose its status code
onServerResponse Object No Called before the server response is sent to the client
onEnter Object No Called when user enters the page
onLeave Object No Called when user leaves the page
viewTransition PageViewTransition No Animate this page's slot when a navigation commits, with a view transition
head Head | ((props: TProps, previous?: Head) => Head) No Head configuration for the page (title, meta tags, etc.)
redirect string No Redirect to another path when this page is matched
label string No Label for the page, used for navigation menus or breadcrumbs

#Examples

Replacing the component of a page declared by a package

typescript
1const alepha = Alepha.create().with(CrmModule);2 3alepha.inject(CrmRouter).clients.override({4  lazy: () => import("./AcmeClients.tsx"),5});

Simple page with data fetching

typescript
 1const userProfile = $page({ 2  path: "/users/:id", 3  schema: { 4    params: z.object({ id: z.integer() }), 5    query: z.object({ tab: z.text().optional() }), 6  }, 7  loader: async ({ params }) => { 8    const user = await userApi.getUser(params.id); 9    return { user };10  },11  lazy: () => import("./UserProfile.tsx"),12});

Nested routing with error handling

typescript
 1const projectSection = $page({ 2  path: "/projects/:id", 3  children: () => [projectBoard, projectSettings], 4  loader: async ({ params }) => { 5    const project = await projectApi.get(params.id); 6    return { project }; 7  }, 8  errorHandler: (error) => { 9    if (isHttpError(error, 404)) {10      return <ProjectNotFound />;11    }12  }13});

Static generation

typescript
 1const blogPost = $page({ 2  path: "/blog/:slug", 3  static: { 4    entries: posts.map((p) => ({ params: { slug: p.slug } })), 5  }, 6  loader: async ({ params }) => { 7    const post = await loadPost(params.slug); 8    return { post }; 9  },10});