@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
61 lines (60 loc) • 2.24 kB
JavaScript
let _tanstack_history = require("@tanstack/history");
let _solidjs_web_storage = require("@solidjs/web/storage");
//#region src/ssr/loadFlightTarget.ts
/**
* The router's half of single-flight collection: runs the matched routes'
* data functions for the URL the client will show after a mutation, then
* hands the loaded router to `collect`. This is the cache-agnostic
* trigger — the router knows how to load route data for a target, and
* deliberately not what that data was written into. Any cache builds a
* flight-data hook from it by composing its own extraction:
*
* ```ts
* registerFlightDataSource('sq', (event, outcome) =>
* loadFlightTarget({
* router: createAppRouter((queryClient = createQueryClient())),
* event,
* outcome,
* collect: () => dehydrate(queryClient),
* }),
* )
* ```
*
* The load observes post-mutation state: the flight event's request
* targets `outcome.targetUrl` with the mutation's cookie effects already
* folded in (a session the mutation just wrote or cleared is what the
* data functions see, exactly as the browser's next request would), and
* the router is pointed at the target through a fresh memory history.
* Resolves undefined when there is no target (a non-browser caller, or a
* redirect leaving the app).
*/
async function loadFlightTarget(options) {
const href = options.href ?? options.outcome.targetUrl;
if (!href) return;
const url = new URL(href);
const request = new Request(url, {
headers: options.outcome.foldedHeaders,
signal: options.outcome.request.signal
});
const flightEvent = {
...options.event,
locals: { ...options.event.locals },
request
};
options.router.update({
history: (0, _tanstack_history.createMemoryHistory)({ initialEntries: [url.pathname + url.search + url.hash] }),
origin: url.origin
});
return await (0, _solidjs_web_storage.provideRequestEvent)(flightEvent, async () => {
try {
await options.router.load({ _signal: request.signal });
return await options.collect(options.router);
} catch (error) {
console.error("Error collecting flight data for the mutation target", error);
return;
}
});
}
//#endregion
exports.loadFlightTarget = loadFlightTarget;
//# sourceMappingURL=loadFlightTarget.cjs.map