@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
1 lines • 4.52 kB
Source Map (JSON)
{"version":3,"file":"loadFlightTarget.cjs","names":[],"sources":["../../../src/ssr/loadFlightTarget.ts"],"sourcesContent":["import { provideRequestEvent } from '@solidjs/web/storage'\nimport { createMemoryHistory } from '@tanstack/history'\nimport type {\n ServerFunctionEvent,\n ServerFunctionOutcome,\n} from '@solidjs/web/server-functions/server'\nimport type { AnyRouter } from '@tanstack/router-core'\n\nexport interface LoadFlightTargetOptions<TRouter extends AnyRouter, T> {\n router: TRouter\n /** The mutation's server-function event (the collector's first argument). */\n event: ServerFunctionEvent\n /** The pre-digested mutation outcome (the collector's second argument). */\n outcome: ServerFunctionOutcome\n /**\n * Overrides the target the router loads, for frameworks whose redirects\n * are serialized before the collector sees them (the pre-digested\n * `outcome.targetUrl` only understands raw `Location` headers). Defaults\n * to `outcome.targetUrl`.\n */\n href?: string\n /**\n * Runs inside the flight request-event scope once the target's route\n * data functions have settled — extract whatever the caller's cache\n * needs (dehydrate a query client, snapshot match state). Returning\n * undefined omits the slice from the response.\n */\n collect: (router: TRouter) => T | Promise<T>\n}\n\n/**\n * The router's half of single-flight collection: runs the matched routes'\n * data functions for the URL the client will show after a mutation, then\n * hands the loaded router to `collect`. This is the cache-agnostic\n * trigger — the router knows how to load route data for a target, and\n * deliberately not what that data was written into. Any cache builds a\n * flight-data hook from it by composing its own extraction:\n *\n * ```ts\n * registerFlightDataSource('sq', (event, outcome) =>\n * loadFlightTarget({\n * router: createAppRouter((queryClient = createQueryClient())),\n * event,\n * outcome,\n * collect: () => dehydrate(queryClient),\n * }),\n * )\n * ```\n *\n * The load observes post-mutation state: the flight event's request\n * targets `outcome.targetUrl` with the mutation's cookie effects already\n * folded in (a session the mutation just wrote or cleared is what the\n * data functions see, exactly as the browser's next request would), and\n * the router is pointed at the target through a fresh memory history.\n * Resolves undefined when there is no target (a non-browser caller, or a\n * redirect leaving the app).\n */\nexport async function loadFlightTarget<TRouter extends AnyRouter, T>(\n options: LoadFlightTargetOptions<TRouter, T>,\n): Promise<T | undefined> {\n const href = options.href ?? options.outcome.targetUrl\n if (!href) {\n return undefined\n }\n\n const url = new URL(href)\n const request = new Request(url, {\n headers: options.outcome.foldedHeaders,\n signal: options.outcome.request.signal,\n })\n const flightEvent: ServerFunctionEvent = {\n ...options.event,\n locals: { ...options.event.locals },\n request,\n }\n\n options.router.update({\n history: createMemoryHistory({\n initialEntries: [url.pathname + url.search + url.hash],\n }),\n origin: url.origin,\n })\n\n return await provideRequestEvent(flightEvent, async () => {\n // Contained, matching Solid Router's own collector: flight data is an\n // optimization, so a failure here must never surface as a mutation\n // error — the slice is simply omitted and the client cache\n // revalidates the normal way.\n try {\n await options.router.load({ _signal: request.signal })\n return await options.collect(options.router)\n } catch (error) {\n console.error(\n 'Error collecting flight data for the mutation target',\n error,\n )\n return undefined\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,eAAsB,iBACpB,SACwB;CACxB,MAAM,OAAO,QAAQ,QAAQ,QAAQ,QAAQ;CAC7C,IAAI,CAAC,MACH;CAGF,MAAM,MAAM,IAAI,IAAI,IAAI;CACxB,MAAM,UAAU,IAAI,QAAQ,KAAK;EAC/B,SAAS,QAAQ,QAAQ;EACzB,QAAQ,QAAQ,QAAQ,QAAQ;CAClC,CAAC;CACD,MAAM,cAAmC;EACvC,GAAG,QAAQ;EACX,QAAQ,EAAE,GAAG,QAAQ,MAAM,OAAO;EAClC;CACF;CAEA,QAAQ,OAAO,OAAO;EACpB,UAAA,GAAA,kBAAA,qBAA6B,EAC3B,gBAAgB,CAAC,IAAI,WAAW,IAAI,SAAS,IAAI,IAAI,EACvD,CAAC;EACD,QAAQ,IAAI;CACd,CAAC;CAED,OAAO,OAAA,GAAA,qBAAA,qBAA0B,aAAa,YAAY;EAKxD,IAAI;GACF,MAAM,QAAQ,OAAO,KAAK,EAAE,SAAS,QAAQ,OAAO,CAAC;GACrD,OAAO,MAAM,QAAQ,QAAQ,QAAQ,MAAM;EAC7C,SAAS,OAAO;GACd,QAAQ,MACN,wDACA,KACF;GACA;EACF;CACF,CAAC;AACH"}