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[]): RouterCreates a client router with history navigation and pattern matching.
Related guidedefineAction#
defineAction<T extends PageAction>(action: T, options?: PageActionOptions | undefined): TDefines a server route action for non-GET requests while preserving handler types.
Related guidedefineLoader#
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 guidedefineMiddleware#
defineMiddleware<T extends PageMiddleware | PageMiddleware[]>(middleware: T, options?: PageMiddlewareOptions | undefined): TDefines route middleware with a stable signature.
Related guidedefineRoutePage#
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 guidedefineServerLoader#
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 guidedefineServerMiddleware#
defineServerMiddleware<T extends PageMiddleware | PageMiddleware[]>(middleware: T): TDefines route middleware that only runs during server-side route resolution.
Related guidegetAvailableRoutes#
getAvailableRoutes(): PageRouteDefinition[]Reads current available routes from the reactive route store.
Related guidegetCurrentPathname#
getCurrentPathname(): stringReads current pathname from the reactive navigation store.
Related guidegetResolvedRoute#
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;
};
} | nullReads the resolved route payload for one pathname or the current active location.
Related guidegetRouteStatus#
getRouteStatus(pathname?: string | undefined): RouteStatusReads current route status state for a pathname.
Related guideisClientRuntime#
isClientRuntime(): booleanReports whether the current value satisfies client runtime for the file routing and route modules.
Related guideisServerRuntime#
isServerRuntime(): booleanReports whether the current value satisfies server runtime for the file routing and route modules.
Related guideLink#
Link(props: PropsWithChildren<LinkProps>): ChildAnchor component with SPA navigation behavior and browser fallback semantics.
Related guideLinkProps#
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 guideLoaderData#
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 guideMiddlewareResult#
type MiddlewareResult = void | {
redirect?: string;
status?: number;
};Describes the result returned by middleware in the file routing and route modules.
Related guideMiddlewareRuntime#
type MiddlewareRuntime = RuntimeEnvironment | "both";Defines the middleware runtime contract used by the file routing and route modules.
Related guidenotFound#
notFound(): neverStops route resolution and renders the application's reserved 404 page.
Related guidePageAction#
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 guidePageActionContext#
type PageActionContext = PageLoadContext;Provides the context passed to page action in the file routing and route modules.
Related guidePageActionOptions#
type PageActionOptions = {
contentType?: ActionContentType;
validateOrigin?: boolean;
};Configures page action in the file routing and route modules.
Related guidePageCachePolicy#
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 guidePageCacheTags#
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 guidePageErrorProps#
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 guidePageHead#
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 guidePageHeadExport#
type PageHeadExport = PageHead | Child;Defines the page head export contract used by the file routing and route modules.
Related guidePageLoadContext#
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 guidePageLoader#
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 guidePageLoaderOptions#
type PageLoaderOptions = {
runtime?: LoaderRuntime;
};Configures page loader in the file routing and route modules.
Related guidePageMiddleware#
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 guidePageMiddlewareOptions#
type PageMiddlewareOptions = {
runtime?: MiddlewareRuntime;
};Configures page middleware in the file routing and route modules.
Related guidePagePendingProps#
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 guidePageProps#
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 guidePageRenderMode#
type PageRenderMode = "ssr" | "csr";Defines the page render mode contract used by the file routing and route modules.
Related guidePageRevalidate#
type PageRevalidate = number | false;Defines the page revalidate contract used by the file routing and route modules.
Related guidePageRouteDefinition#
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 guidePageStaticParams#
type PageStaticParams = RouterParams[];Defines the page static params contract used by the file routing and route modules.
Related guideprefetchRoute#
prefetchRoute(pathname: string, options?: { signal?: AbortSignal | undefined; } | undefined): Promise<void>Prefetches a route by resolving its loaders without changing browser location.
Related guideRouteConfig#
type RouteConfig = {
path: string;
component: Component<{
params: RouterParams;
}>;
};Defines the route config contract used by the file routing and route modules.
Related guideRouteParamsFromPath#
type RouteParamsFromPath<TPath extends string> = RouteParamsFromSegments<TPath>;Defines the route params from path contract used by the file routing and route modules.
Related guideRouter#
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 guideRouterParams#
type RouterParams = Record<string, string>;Defines the router params contract used by the file routing and route modules.
Related guideRouterProvider#
RouterProvider(props: PropsWithChildren<RouterProviderProps>): ChildProvides router context and renders either explicit children or matched route view.
Related guideRouteStatus#
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 guideRouteStatusState#
type RouteStatusState = {
byPath: Record<string, RouteStatus>;
};Represents the observable state of route status in the file routing and route modules.
Related guidesubscribeAvailableRoutes#
subscribeAvailableRoutes(listener: (routes: PageRouteDefinition[]) => void): () => voidSubscribes to route catalog updates.
Related guidesubscribePathname#
subscribePathname(listener: (pathname: string) => void): () => voidSubscribes to pathname changes.
Related guidesubscribeRouteStatus#
subscribeRouteStatus(listener: (status: RouteStatus, all: RouteStatusState) => void, pathname?: string | undefined): () => voidSubscribes to route status changes for one pathname or all routes.
Related guideTypedPageModule#
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