Navigated to /docs/core/api/router

Router and Auto Pages API

Routes, navigation, loaders, middleware, actions, route modules, status, prefetching, and subscriptions.

Router and Auto Pages exports

52 public exports, grouped under their canonical import boundary.

@tavojs/core/router

Canonical import boundary for every symbol in this section.

createRouter#

createRouter(routes: RouteConfig[]): Router

Creates a client router with history navigation and pattern matching.

Related guide

defineAction#

defineAction<T extends PageAction>(action: T, options?: PageActionOptions | undefined): T

Defines a server route action for non-GET requests while preserving handler types.

Related guide

defineLoader#

defineLoader<T>(loader: (context: PageLoadContext) => T | Promise<T>, options?: PageLoaderOptions | undefined): (context: PageLoadContext) => T | Promise<T>

Defines a route loader while preserving its return type for app-level helpers.

Related guide

defineMiddleware#

defineMiddleware<T extends PageMiddleware | PageMiddleware[]>(middleware: T, options?: PageMiddlewareOptions | undefined): T

Defines route middleware with a stable signature.

Related guide

defineRoutePage#

defineRoutePage<TPath extends string, TData = unknown>(path: TPath, page: TypedPageModule<TPath, TData>): TypedPageModule<TPath, TData>

Defines a page module with a filename path hint for typed params and loader data.

Related guide

defineServerLoader#

defineServerLoader<T>(loader: (context: PageLoadContext) => T | Promise<T>): (context: PageLoadContext) => T | Promise<T>

Defines a route loader that only runs during server-side route resolution.

Related guide

defineServerMiddleware#

defineServerMiddleware<T extends PageMiddleware | PageMiddleware[]>(middleware: T): T

Defines route middleware that only runs during server-side route resolution.

Related guide

getAvailableRoutes#

getAvailableRoutes(): PageRouteDefinition[]

Reads current available routes from the reactive route store.

Related guide

getCurrentPathname#

getCurrentPathname(): string

Reads current pathname from the reactive navigation store.

Related guide

getResolvedRoute#

getResolvedRoute(pathname?: string): {
    pathname: string;
    params: RouterParams;
    route: PageRouteDefinition | null;
    status: number;
    data: unknown;
    error: unknown;
    layers: RouteDataLayer[];
    layerData: Record<string, unknown>;
    head: PageHead;
    cache: PageCachePolicy;
    renderMode: PageRenderMode;
    node: Child;
    redirect?: string;
    i18n?: {
        locale: string;
        dir: I18nLocaleDirection;
    };
} | null

Reads the resolved route payload for one pathname or the current active location.

Related guide

getRouteStatus#

getRouteStatus(pathname?: string | undefined): RouteStatus

Reads current route status state for a pathname.

Related guide

isClientRuntime#

isClientRuntime(): boolean

Reports whether the current value satisfies client runtime for the file routing and route modules.

Related guide

isServerRuntime#

isServerRuntime(): boolean

Reports whether the current value satisfies server runtime for the file routing and route modules.

Related guide

LinkProps#

type LinkProps = {
    to: string;
    replace?: boolean;
    scroll?: boolean;
    className?: ClassName;
    children?: Child;
};

Defines the props accepted by link in the file routing and route modules.

Related guide

LoaderData#

type LoaderData<TLoader> = TLoader extends (...args: any[]) => infer TResult ? Awaited<TResult> : never;

Defines the loader data contract used by the file routing and route modules.

Related guide

MiddlewareResult#

type MiddlewareResult = void | {
    redirect?: string;
    status?: number;
};

Describes the result returned by middleware in the file routing and route modules.

Related guide

MiddlewareRuntime#

type MiddlewareRuntime = RuntimeEnvironment | "both";

Defines the middleware runtime contract used by the file routing and route modules.

Related guide

navigate#

navigate(to: string, options?: RouterNavigateOptions | undefined): void

Navigates using active router when available, with history fallback.

Related guide

notFound#

notFound(): never

Stops route resolution and renders the application's reserved 404 page.

Related guide

PageAction#

type PageAction = ((context: PageActionContext) => Response | ActionResult | void | Promise<Response | ActionResult | void>) & {
    __tavo_action_options__?: PageActionOptions;
};

Defines the page action contract used by the file routing and route modules.

Related guide

PageActionContext#

type PageActionContext = PageLoadContext;

Provides the context passed to page action in the file routing and route modules.

Related guide

PageActionOptions#

type PageActionOptions = {
    contentType?: ActionContentType;
    validateOrigin?: boolean;
};

Configures page action in the file routing and route modules.

Related guide

PageCachePolicy#

type PageCachePolicy = {
    static: boolean;
    revalidate: number | null;
    vary: string[];
    tags: string[];
};

Defines the page cache policy contract used by the file routing and route modules.

Related guide

PageCacheTags#

type PageCacheTags = string | string[] | ((context: PageLoadContext) => string | string[] | Promise<string | string[]>);

Defines the page cache tags contract used by the file routing and route modules.

Related guide

PageErrorProps#

type PageErrorProps<TParams extends RouterParams = RouterParams, TLayers extends Record<string, unknown> = Record<string, unknown>> = PagePendingProps<TParams, TLayers> & {
    data: unknown;
    error: unknown;
};

Defines the props accepted by page error in the file routing and route modules.

Related guide

PageHead#

type PageHead = {
    title?: string;
    unsafeHeadHtml?: string;
    status?: number;
    htmlAttributes?: Record<string, string | number | boolean>;
    bodyAttributes?: Record<string, string | number | boolean>;
};

Defines the page head contract used by the file routing and route modules.

Related guide

PageHeadExport#

type PageHeadExport = PageHead | Child;

Defines the page head export contract used by the file routing and route modules.

Related guide

PageLoadContext#

type PageLoadContext = {
    pathname: string;
    params: RouterParams;
    request: Request;
    rawRequest?: unknown;
    url: URL;
    headers: Headers;
    method: string;
    signal: AbortSignal;
    layers?: Record<string, unknown>;
};

Provides the context passed to page load in the file routing and route modules.

Related guide

PageLoader#

type PageLoader = ((context: PageLoadContext) => unknown | Promise<unknown>) & {
    __tavo_loader_options__?: PageLoaderOptions;
};

Defines the page loader contract used by the file routing and route modules.

Related guide

PageLoaderOptions#

type PageLoaderOptions = {
    runtime?: LoaderRuntime;
};

Configures page loader in the file routing and route modules.

Related guide

PageMiddleware#

type PageMiddleware = ((context: {
    to: string;
    from?: string;
    params: RouterParams;
    request: Request;
    rawRequest?: unknown;
    url: URL;
    headers: Headers;
    method: string;
    signal: AbortSignal;
}) => MiddlewareResult | Promise<MiddlewareResult>) & {
    __tavo_middleware_options__?: PageMiddlewareOptions;
};

Defines the page middleware contract used by the file routing and route modules.

Related guide

PageMiddlewareOptions#

type PageMiddlewareOptions = {
    runtime?: MiddlewareRuntime;
};

Configures page middleware in the file routing and route modules.

Related guide

PagePendingProps#

type PagePendingProps<TParams extends RouterParams = RouterParams, TLayers extends Record<string, unknown> = Record<string, unknown>> = {
    pathname: string;
    params: TParams;
    layers: RouteDataLayer[];
    layerData: TLayers;
};

Defines the props accepted by page pending in the file routing and route modules.

Related guide

PageProps#

type PageProps<TData = unknown, TParams extends RouterParams = RouterParams, TLayers extends Record<string, unknown> = Record<string, unknown>> = {
    pathname?: string;
    params: TParams;
    data?: TData;
    error?: unknown;
    layers?: RouteDataLayer[];
    layerData?: TLayers;
};

Defines the props accepted by page in the file routing and route modules.

Related guide

PageRenderMode#

type PageRenderMode = "ssr" | "csr";

Defines the page render mode contract used by the file routing and route modules.

Related guide

PageRevalidate#

type PageRevalidate = number | false;

Defines the page revalidate contract used by the file routing and route modules.

Related guide

PageRouteDefinition#

type PageRouteDefinition = {
    file: string;
    path: string;
    component: Component<AnyRecord>;
    pending?: Component<AnyRecord>;
    error?: Component<AnyRecord>;
    layouts: Component<AnyRecord>[];
    layoutLayers: Array<{
        kind: "root" | "layout";
        id: string;
        file: string;
        component: Component<AnyRecord>;
        load?: PageModuleRecord["load"];
        head?: PageModuleRecord["head"];
        middleware: PageMiddleware[];
        render?: PageModuleRecord["render"];
        layout?: PageModuleRecord["layout"];
        prerender?: boolean;
        static?: boolean;
        revalidate?: PageRevalidate;
        vary?: string | string[];
        cacheTags?: PageCacheTags;
    }>;
    load?: PageModuleRecord["load"];
    action?: PageModuleRecord["action"];
    head?: PageModuleRecord["head"];
    middleware: PageMiddleware[];
    cacheTags?: PageCacheTags;
    cacheTagResolvers: PageCacheTags[];
    generateStaticParams?: PageModuleRecord["generateStaticParams"];
    renderMode: PageRenderMode;
    cache: PageCachePolicy;
};

Defines the page route definition contract used by the file routing and route modules.

Related guide

PageStaticParams#

type PageStaticParams = RouterParams[];

Defines the page static params contract used by the file routing and route modules.

Related guide

prefetchRoute#

prefetchRoute(pathname: string, options?: { signal?: AbortSignal | undefined; } | undefined): Promise<void>

Prefetches a route by resolving its loaders without changing browser location.

Related guide

RouteConfig#

type RouteConfig = {
    path: string;
    component: Component<{
        params: RouterParams;
    }>;
};

Defines the route config contract used by the file routing and route modules.

Related guide

RouteParamsFromPath#

type RouteParamsFromPath<TPath extends string> = RouteParamsFromSegments<TPath>;

Defines the route params from path contract used by the file routing and route modules.

Related guide

Router#

type Router = {
    navigate(to: string, options?: RouterNavigateOptions): void;
    getPathname(): string;
    match(pathname: string): {
        route: RouteConfig | null;
        params: RouterParams;
    };
};

Defines the router contract used by the file routing and route modules.

Related guide

RouterNavigateOptions#

type RouterNavigateOptions = {
    replace?: boolean;
    scroll?: boolean;
};

Configures router navigate in the file routing and route modules.

Related guide

RouterParams#

type RouterParams = Record<string, string>;

Defines the router params contract used by the file routing and route modules.

Related guide

RouterProvider#

RouterProvider(props: PropsWithChildren<RouterProviderProps>): Child

Provides router context and renders either explicit children or matched route view.

Related guide

RouteStatus#

type RouteStatus = {
    pathname: string;
    status: "idle" | "loading" | "prefetching" | "ready" | "redirecting" | "error";
    error: unknown;
    redirect?: string;
};

Defines the route status contract used by the file routing and route modules.

Related guide

RouteStatusState#

type RouteStatusState = {
    byPath: Record<string, RouteStatus>;
};

Represents the observable state of route status in the file routing and route modules.

Related guide

subscribeAvailableRoutes#

subscribeAvailableRoutes(listener: (routes: PageRouteDefinition[]) => void): () => void

Subscribes to route catalog updates.

Related guide

subscribePathname#

subscribePathname(listener: (pathname: string) => void): () => void

Subscribes to pathname changes.

Related guide

subscribeRouteStatus#

subscribeRouteStatus(listener: (status: RouteStatus, all: RouteStatusState) => void, pathname?: string | undefined): () => void

Subscribes to route status changes for one pathname or all routes.

Related guide

TypedPageModule#

type TypedPageModule<TPath extends string, TData = unknown> = Omit<PageModuleRecord, "default" | "pending" | "error" | "prerender"> & {
    default: Component<PageProps<TData, RouteParamsFromPath<TPath>>>;
    pending?: Component<PagePendingProps<RouteParamsFromPath<TPath>>>;
    error?: Component<PageErrorProps<RouteParamsFromPath<TPath>>>;
};

Defines the typed page module contract used by the file routing and route modules.

Related guide