@tanstack/router-core
Version:
Modern and scalable routing for React applications
1 lines • 3.75 kB
Source Map (JSON)
{"version":3,"file":"createRequestHandler.cjs","names":[],"sources":["../../../src/ssr/createRequestHandler.ts"],"sourcesContent":["import { createMemoryHistory } from '@tanstack/history'\nimport { mergeHeaders } from './headers'\nimport {\n attachRouterServerSsrUtils,\n getNormalizedURL,\n getOrigin,\n} from './ssr-server'\nimport type { HandlerCallback } from './handlerCallback'\nimport type { AnyHeaders } from './headers'\nimport type { AnyRouter } from '../router'\nimport type { Manifest } from '../manifest'\n\nexport type RequestHandler<TRouter extends AnyRouter> = (\n cb: HandlerCallback<TRouter>,\n) => Promise<Response>\n\nexport function createRequestHandler<TRouter extends AnyRouter>({\n createRouter,\n request,\n getRouterManifest,\n}: {\n createRouter: () => TRouter\n request: Request\n getRouterManifest?: () => Manifest | Promise<Manifest>\n}): RequestHandler<TRouter> {\n return async (cb) => {\n const router = createRouter()\n // Track whether the callback will handle cleanup\n let cbWillCleanup = false\n\n try {\n attachRouterServerSsrUtils({\n router,\n manifest: await getRouterManifest?.(),\n })\n\n // normalizing and sanitizing the pathname here for server, so we always deal with the same format during SSR.\n const { url } = getNormalizedURL(request.url, 'http://localhost')\n const origin = getOrigin(request)\n const href = url.href.replace(url.origin, '')\n\n // Create a history for the router\n const history = createMemoryHistory({\n initialEntries: [href],\n })\n\n // Update the router with the history and context\n router.update({\n history,\n origin: router.options.origin ?? origin,\n })\n\n await router.load()\n\n await router.serverSsr?.dehydrate()\n\n const responseHeaders = getRequestHeaders({\n router,\n })\n\n // Mark that the callback will handle cleanup\n cbWillCleanup = true\n return cb({\n request,\n router,\n responseHeaders,\n })\n } finally {\n if (!cbWillCleanup) {\n // Clean up router SSR state if the callback won't handle it\n // (e.g., if an error occurred before the callback was invoked).\n // When the callback runs, it handles cleanup (either via transformStreamWithRouter\n // for streaming, or directly in renderRouterToString for non-streaming).\n router.serverSsr?.cleanup()\n }\n }\n }\n}\n\nfunction getRequestHeaders(opts: { router: AnyRouter }): Headers {\n const matchHeaders = opts.router.state.matches.map<AnyHeaders>(\n (match) => match.headers,\n )\n\n // Handle Redirects\n const { redirect } = opts.router.state\n if (redirect) {\n matchHeaders.push(redirect.headers)\n }\n\n return mergeHeaders(\n {\n 'Content-Type': 'text/html; charset=UTF-8',\n },\n ...matchHeaders,\n )\n}\n"],"mappings":";;;;;AAgBA,SAAgB,qBAAgD,EAC9D,cACA,SACA,qBAK0B;AAC1B,QAAO,OAAO,OAAO;EACnB,MAAM,SAAS,cAAc;EAE7B,IAAI,gBAAgB;AAEpB,MAAI;AACF,sBAAA,2BAA2B;IACzB;IACA,UAAU,MAAM,qBAAqB;IACtC,CAAC;GAGF,MAAM,EAAE,QAAQ,mBAAA,iBAAiB,QAAQ,KAAK,mBAAmB;GACjE,MAAM,SAAS,mBAAA,UAAU,QAAQ;GAIjC,MAAM,WAAA,GAAA,kBAAA,qBAA8B,EAClC,gBAAgB,CAJL,IAAI,KAAK,QAAQ,IAAI,QAAQ,GAAG,CAIrB,EACvB,CAAC;AAGF,UAAO,OAAO;IACZ;IACA,QAAQ,OAAO,QAAQ,UAAU;IAClC,CAAC;AAEF,SAAM,OAAO,MAAM;AAEnB,SAAM,OAAO,WAAW,WAAW;GAEnC,MAAM,kBAAkB,kBAAkB,EACxC,QACD,CAAC;AAGF,mBAAgB;AAChB,UAAO,GAAG;IACR;IACA;IACA;IACD,CAAC;YACM;AACR,OAAI,CAAC,cAKH,QAAO,WAAW,SAAS;;;;AAMnC,SAAS,kBAAkB,MAAsC;CAC/D,MAAM,eAAe,KAAK,OAAO,MAAM,QAAQ,KAC5C,UAAU,MAAM,QAClB;CAGD,MAAM,EAAE,aAAa,KAAK,OAAO;AACjC,KAAI,SACF,cAAa,KAAK,SAAS,QAAQ;AAGrC,QAAO,gBAAA,aACL,EACE,gBAAgB,4BACjB,EACD,GAAG,aACJ"}