@tanstack/solid-start
Version:
Modern and scalable routing for Solid applications
80 lines (79 loc) • 3.1 kB
JavaScript
import { getRouterInstance } from "@tanstack/start-client-core";
import { subscribeFlightData } from "@solidjs/web/server-functions/client";
//#region src/solid-rpc-flight-client.ts
var subscribed = false;
function subscribeSolidStartFlightData() {
if (subscribed) return;
subscribed = true;
subscribeFlightData(async (data) => {
if (!isSolidStartFlightData(data)) return;
const router = await getRouterInstance();
if ("dehydratedData" in data) await router.options.hydrate?.(data.dehydratedData);
const currentLocation = isCurrentLocation(router, data.href);
const matches = currentLocation ? router.stores.matches.get() : router.matchRoutes(router.buildLocation({ href: data.href }));
if (matches.length !== data.matches.length) return;
const flightMatches = new Map(data.matches.map((match) => [match.id, match]));
const hydratedMatches = [];
for (const match of matches) {
const flightMatch = flightMatches.get(match.id);
if (!flightMatch) return;
hydratedMatches.push(applyFlightMatch(match, flightMatch));
}
if (currentLocation) publishCurrentMatches(router, hydratedMatches);
else seedRedirectMatches(router, hydratedMatches);
});
}
function applyFlightMatch(match, flightMatch) {
if (!(flightMatch.loaderData !== void 0 || flightMatch.beforeLoadContext !== void 0 || "error" in flightMatch || flightMatch.notFound === true || match.error !== void 0 || match._notFound === true)) return match;
const nextMatch = {
...match,
error: flightMatch.error,
invalid: false,
isFetching: false,
preload: false,
status: flightMatch.status,
updatedAt: flightMatch.updatedAt,
_flight: void 0,
_notFound: flightMatch.notFound
};
if ("loaderData" in flightMatch) nextMatch.loaderData = flightMatch.loaderData;
if ("beforeLoadContext" in flightMatch) {
nextMatch.__beforeLoadContext = flightMatch.beforeLoadContext;
nextMatch.context = {
...nextMatch.context,
...flightMatch.beforeLoadContext
};
}
if ("ssr" in flightMatch) nextMatch.ssr = flightMatch.ssr;
return nextMatch;
}
function publishCurrentMatches(router, matches) {
for (const match of matches) router._cache.delete(match.id);
router._committed = matches;
router.batch(() => {
router.stores.setMatches(matches);
router.stores.status.set("idle");
router.stores.resolvedLocation.set(router.stores.location.get());
});
}
function seedRedirectMatches(router, matches) {
for (const match of matches) router._cache.set(match.id, {
...match,
context: {},
preload: true
});
}
function isCurrentLocation(router, href) {
const current = new URL(router.latestLocation.href, window.location.href);
const target = new URL(href, current);
return current.pathname === target.pathname && current.search === target.search && current.hash === target.hash;
}
function isSolidStartFlightData(value) {
return isObject(value) && typeof value.href === "string" && Array.isArray(value.matches);
}
function isObject(value) {
return !!value && typeof value === "object";
}
//#endregion
export { subscribeSolidStartFlightData };
//# sourceMappingURL=solid-rpc-flight-client.js.map