INDIE UNPKG

4.86 kBTypeScriptView Raw
1
2import { PatchRoutesOnNavigationFunction, RouteObject } from "../../router/utils.js";
3import { Router } from "../../router/router.js";
4import { ClientOnErrorFunction } from "../../components.js";
5import { RouteModules } from "./routeModules.js";
6import { RouterFetch } from "./single-fetch.js";
7import { WindowReactRouterContext } from "../global.js";
8import { AssetsManifest } from "./entry.js";
9
10//#region lib/dom/ssr/route-discovery.d.ts
11/** A discovery response from a different build. */
12type ManifestMismatchEvent = {
13 source: "eager" | "navigation" | "fetcher" | "imperative";
14 version: string;
15 reloadUrl: string | null;
16 loadAllRoutes(): Promise<void>;
17 stopPropagation(): void; /** Run built-in recovery for this source, at most once per mismatch event. */
18 defaultBehavior(): Promise<void>;
19};
20/** The result of imperative route discovery. */
21type DiscoveryResult = {
22 type: "success";
23} | {
24 type: "version-mismatch";
25} | {
26 type: "aborted";
27};
28/** Options for {@link unstable_useRouteDiscovery}. */
29type RouteDiscoveryOptions = {
30 onManifestMismatch?: (event: ManifestMismatchEvent) => void | Promise<void>;
31};
32type Registration = {
33 current: RouteDiscoveryOptions;
34};
35type DiscoveryState = "partial" | "loading" | "complete";
36type RouteDiscoverySsrInfo = {
37 manifest: AssetsManifest;
38 routeModules: RouteModules;
39 router: Router | undefined;
40 context: Pick<WindowReactRouterContext, "ssr" | "routeDiscovery" | "isSpaMode" | "basename" | "unstable_apiServerOrigin" | "unstable_apiOnly">;
41};
42declare class RouteDiscoveryRuntime {
43 private ssrInfo;
44 readonly enabled: boolean;
45 readonly discoveredPaths: Set<string>;
46 readonly nextPaths: Set<string>;
47 state: DiscoveryState;
48 ready: boolean;
49 private registrations;
50 private listeners;
51 private fullLoadErrorListeners;
52 private fullLoad?;
53 private loadedManifest?;
54 private mismatch?;
55 private fetchImplementation;
56 private loadManifest;
57 onError?: ClientOnErrorFunction;
58 constructor(ssrInfo: RouteDiscoverySsrInfo, {
59 fetch: fetchImplementation,
60 loadManifest
61 }?: {
62 fetch?: RouterFetch;
63 loadManifest?: () => Promise<AssetsManifest>;
64 });
65 private get router();
66 private loadManifestAsset;
67 subscribe: (listener: () => void) => () => void;
68 private update;
69 markReady: () => void;
70 register(registration: Registration): () => void;
71 private isComplete;
72 loadAllRoutes: () => Promise<void>;
73 reportError(error: unknown): void;
74 private dispatchMismatch;
75 private requestUrl;
76 discoverRoutes: (paths: string[], options?: {
77 signal?: AbortSignal;
78 }) => Promise<DiscoveryResult>;
79 discover(paths: string[], source: ManifestMismatchEvent["source"], reloadUrl: string | null, signal?: AbortSignal, patch?: (routeId: string | null, children: RouteObject[], unstable_allowElementMutations?: boolean) => void): Promise<DiscoveryResult>;
80}
81/**
82 * Control lazy route discovery in Framework Mode with `future.unstable_customRouteDiscovery`
83 * enabled. This allows you to imperatively discover routes as well as add custom behavior
84 * when a manifest version mismatch is detected. Calling `loadAllRoutes` will load the entire
85 * route manifest for the current (potentially stale) client version - this requires that prior
86 * client artifacts are still available on your CDN _and_ you have not made breaking changes to
87 * the API contracts of your loaders and actions.
88 *
89 * See the [Route Discovery guide](../../how-to/route-discovery) for more information and examples.
90 *
91 * @example
92 * let { state, discoverRoutes, loadAllRoutes } = unstable_useRouteDiscovery({
93 * onManifestMismatch: async (event) => {
94 * // Eagerly load the complete route tree for this client without requiring a hard reload
95 * await event.loadAllRoutes();
96 * },
97 * });
98 *
99 * // Eagerly discover specific routes
100 * discoverRoutes(["/a", "/b"]);
101 *
102 * @name unstable_useRouteDiscovery
103 * @public
104 * @category Hooks
105 * @mode framework
106 * @param options An awaited manifest-mismatch handler.
107 * @returns The client version, full-manifest loading state, and discovery operations.
108 */
109declare function useRouteDiscovery(options?: RouteDiscoveryOptions): {
110 version: string;
111 state: DiscoveryState;
112 loadAllRoutes: () => Promise<void>;
113 discoverRoutes: (paths: string[], options?: {
114 signal?: AbortSignal;
115 }) => Promise<DiscoveryResult>;
116};
117declare function getCustomPatchRoutesOnNavigationFunction(getRouter: () => Router, runtime: RouteDiscoveryRuntime): PatchRoutesOnNavigationFunction | undefined;
118declare function useCustomRouteDiscovery(runtime: RouteDiscoveryRuntime, onError?: ClientOnErrorFunction): "partial" | "loading" | "complete";
119//#endregion
120export { DiscoveryResult, DiscoveryState, ManifestMismatchEvent, RouteDiscoveryOptions, RouteDiscoveryRuntime, getCustomPatchRoutesOnNavigationFunction, useCustomRouteDiscovery, useRouteDiscovery };
\No newline at end of file