/** * Client-side navigation for Ruvyxa. * * Ruvyxa route bundles already know how to re-render into an existing root: * the generated entry calls `__RUVYXA_ROOT__.render(...)` when one exists and * `hydrateRoot(...)` when it does not. This module supplies the missing half — * a route table, history integration, and bundle loading — so a link click can * swap pages without a document load. * * ## Contract with the generated entry * * Every client route bundle publishes two globals (see `build_entry_source` in * `crates/ruvyxa_bundler/src/output.rs` and `clientEntrySource` in * `packages/ruvyxa/runtime/entry-templates.mjs`): * * - `globalThis.__RUVYXA_ROUTE_CONTEXT__` — the React context the routing * hooks read. It is created on `globalThis` rather than imported because a * generated entry cannot depend on this package; an app may render plain * React pages with no Ruvyxa components at all. * - `globalThis.__RUVYXA_ROUTES__[routePath]` — a function from a route * context to the composed element tree, registered when the bundle executes. * * A route whose bundle has already executed is re-rendered from that registry * directly. `import()` caches by URL and would not re-run the bundle, so * navigating away and back would otherwise render nothing. */ import { type RouteParams } from '@ruvyxa/core/route-match'; import type { FlightValue } from '@ruvyxa/core/server'; import type { RouteHref } from './route-types.js'; /** * The active route, as seen by the routing hooks. * * Deliberately excludes the query string. A server render has no reliable way * to know it in every deployment target, so putting it here would make the * provider value differ between the server HTML and the first client render — * a hydration mismatch. `useSearchParams` reads it from an external store with * an empty server snapshot instead, which React resolves after hydration. */ export interface RouteContextValue { /** Pathname of the current URL, without search or hash. */ pathname: string; /** Parameters extracted from the matched route pattern. */ params: RouteParams; /** The matched route pattern, e.g. `/blog/[slug]`. */ route: string; /** Public server-component data for this exact route artifact, when enabled. */ flight?: FlightValue; /** * The intercepting route showing over this one, when a soft navigation * opened it. * * Absent on the server and on a hard load, which is what makes a refresh * render the intercepted URL's real page instead of the overlay. */ intercept?: RouteInterceptState; } /** An intercepting route the client router has opened over the mounted page. */ export interface RouteInterceptState { /** Route id of the level whose layout holds the slot (`app/feed`). */ level: string; /** Slot name the overlay replaces. */ name: string; /** Route pattern the interception covers. */ target: string; /** Parameters matched from the intercepted URL. */ params: RouteParams; /** The intercepted URL's pathname. */ path: string; } /** Options accepted by the imperative navigation methods. */ export interface NavigateOptions { /** Replace the current history entry instead of pushing a new one. */ replace?: boolean; /** Scroll to the top after navigating. Defaults to `true`. */ scroll?: boolean; /** Animate this navigation with the browser View Transitions API when available. */ viewTransition?: boolean; } /** * Public navigation surface returned by {@link useRouter}. * * `href` is narrowed to the project's real routes once * `.ruvyxa/types/routes.d.ts` is generated and included by `tsconfig.json`, and * is plain `string` otherwise. `RouterInstance` below stays untyped in that * respect on purpose: it is the engine seam `` and the hooks call, and it * must accept the URLs the browser hands back on `popstate`. */ export interface RuvyxaRouter { push(href: RouteHref, options?: NavigateOptions): Promise; replace(href: RouteHref, options?: NavigateOptions): Promise; back(): void; forward(): void; /** Re-render the current route from its already-loaded bundle. */ refresh(): void; /** * Re-fetch the current route's server payload, then re-render it. * * `refresh()` re-runs the client tree against the payload it already has, so * it cannot recover from a server-side failure. This discards that payload * first, which is what an `error.tsx` retry button needs: the render failed * because of what the server sent (or did not send), and rendering the same * bytes again would fail the same way. */ retry(): Promise; /** Warm a route's bundle so a later navigation renders immediately. */ prefetch(href: RouteHref): void; /** `true` while a navigation is loading a bundle. */ readonly pending: boolean; } /** Internal navigation singleton shared by the routing hooks and ``. */ export interface RouterInstance { subscribe(listener: () => void): () => void; getSnapshot(): RouteContextValue; /** Live query string, including the leading `?`. Empty outside a browser. */ getSearch(): string; getPending(): boolean; navigate(href: string, options: NavigateOptions & { history?: 'push' | 'replace' | 'none'; }): Promise; prefetch(href: string): void; refresh(): void; retry(): Promise; } /** * What `href` turned out to be. * * `internal` is a same-origin page this router can render itself; `external` is * a real navigation the browser must perform; `refused` is not a navigation at * all. Separating the last two matters: the old `URL | null` answer conflated * "not mine, hand it to the browser" with "not a navigation", and the * fall-through then replayed the caller's *raw string* into * `location.assign` — which executes a `javascript:` URL in this document. */ export type NavigationTarget = { kind: 'internal'; url: URL; } | { kind: 'external'; url: URL; } /** * `reason` is absent only when there is nothing to report — no browser to * navigate in at all. Every refusal a caller could have avoided carries one, * which is what lets the classifier stay silent and the callers that *asked * for a navigation* do the reporting. */ | { kind: 'refused'; reason?: string; }; /** * Classify `href` against the current document, reporting nothing. * * Cross-origin links, downloads, and `mailto:`/`tel:`/`sms:` must reach the * browser untouched; intercepting them would break links that have always * worked. Everything else is refused rather than passed along, because * `useRouter().push()` is a sink an `` click is not: without the router * there is no way for a page to turn a data string into a navigation. * * Exported for ``, which has to ask the question *before* it calls * `preventDefault()`: a click it suppresses and the router then refuses is a * link that does nothing at all. It asks silently because a refusal there is * not one — the anchor still carries the href and the browser goes on to * handle it, exactly as it did before this router existed. Not part of the * package's public surface: `index.ts` does not re-export it and the * `exports` map does not expose this module. */ export declare function classifyNavigationTarget(href: string): NavigationTarget; /** * Read one Flight payload, or throw. * * Exported for `test/flight-decoder.test.mjs` rather than for callers: this is * the decoder every browser runs, it is the second implementation of a format * `packages/ruvyxa/runtime/flight.mjs` also decodes, and it had no test of any * kind while the round trip beside the encoder made the format look covered. * It is not re-exported from the package index, so it is not public API. */ export declare function decodeFlight(payload: string, artifactVersion: string, pathname: string): FlightValue; /** * The router singleton for this document. * * Kept on `globalThis` so a route bundle and the app's own copy of this * package share one instance even if they were bundled separately. */ export declare function getRouterInstance(): RouterInstance; //# sourceMappingURL=router.d.ts.map