everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
60 lines (58 loc) • 2.24 kB
JavaScript
import { createAuthClient } from "./auth.mjs";
import { defaultNotFoundComponent, defaultPendingComponent } from "./router-defaults.mjs";
import { RouterError } from "./router-error.mjs";
import { createBrowserHistory, createRouter as createRouter$1 } from "@tanstack/react-router";
import { dehydrate, hydrate } from "@tanstack/react-query";
//#region src/ui/router-client.tsx
/**
* Client router factory — creates a TanStack Router with browser history.
* The app-specific route tree is injected as `defaultRouteTree` (the
* bundled core-only fallback); the composed tree wins when manifest
* construction produced one.
*/
function createRouter(opts) {
const queryClient = opts.context.queryClient;
const history = opts.history ?? createBrowserHistory();
const cspNonce = opts.context.cspNonce;
const routeTree = opts.routeTree ?? opts.defaultRouteTree;
if (!routeTree) throw new Error("createRouter: no route tree — compose failed and no core-only fallback was constructed");
return {
router: createRouter$1({
routeTree,
history,
basepath: opts.basepath ?? opts.context.runtimeConfig?.runtime?.runtimeBasePath ?? "/",
context: {
queryClient,
runtimeConfig: opts.context.runtimeConfig,
cspNonce: opts.context.cspNonce,
apiClient: opts.context.apiClient,
authClient: opts.context.authClient ?? createAuthClient({
runtimeConfig: opts.context.runtimeConfig,
cspNonce: opts.context.cspNonce
}),
session: opts.context.session,
locale: opts.context.locale
},
...cspNonce ? { ssr: { nonce: cspNonce } } : {},
defaultPreload: "intent",
scrollRestoration: true,
defaultStructuralSharing: true,
defaultPreloadStaleTime: 0,
defaultPendingMinMs: 0,
defaultErrorComponent: RouterError,
defaultNotFoundComponent,
defaultPendingComponent,
dehydrate: () => {
if (typeof window === "undefined") return { queryClientState: dehydrate(queryClient) };
return { queryClientState: {} };
},
hydrate: (dehydrated) => {
if (typeof window !== "undefined" && dehydrated?.queryClientState) hydrate(queryClient, dehydrated.queryClientState);
}
}),
queryClient
};
}
//#endregion
export { createRouter };
//# sourceMappingURL=router-client.mjs.map