@tanstack/react-router
Version:
Modern and scalable routing for React applications
1 lines • 3.75 kB
Source Map (JSON)
{"version":3,"file":"renderRouterToStream.cjs","sources":["../../../src/ssr/renderRouterToStream.tsx"],"sourcesContent":["import { PassThrough } from 'node:stream'\nimport ReactDOMServer from 'react-dom/server'\nimport { isbot } from 'isbot'\nimport {\n transformPipeableStreamWithRouter,\n transformReadableStreamWithRouter,\n} from '@tanstack/router-core/ssr/server'\nimport type { ReadableStream } from 'node:stream/web'\nimport type { AnyRouter } from '@tanstack/router-core'\nimport type { ReactNode } from 'react'\n\nexport const renderRouterToStream = async ({\n request,\n router,\n responseHeaders,\n children,\n}: {\n request: Request\n router: AnyRouter\n responseHeaders: Headers\n children: ReactNode\n}) => {\n if (typeof ReactDOMServer.renderToReadableStream === 'function') {\n const stream = await ReactDOMServer.renderToReadableStream(children, {\n signal: request.signal,\n })\n\n if (isbot(request.headers.get('User-Agent'))) {\n await stream.allReady\n }\n\n const responseStream = transformReadableStreamWithRouter(\n router,\n stream as unknown as ReadableStream,\n )\n return new Response(responseStream as any, {\n status: router.state.statusCode,\n headers: responseHeaders,\n })\n }\n\n if (typeof ReactDOMServer.renderToPipeableStream === 'function') {\n const reactAppPassthrough = new PassThrough()\n\n try {\n const pipeable = ReactDOMServer.renderToPipeableStream(children, {\n ...(isbot(request.headers.get('User-Agent'))\n ? {\n onAllReady() {\n pipeable.pipe(reactAppPassthrough)\n },\n }\n : {\n onShellReady() {\n pipeable.pipe(reactAppPassthrough)\n },\n }),\n onError: (error, info) => {\n if (error instanceof Error && error.message === 'ShellBoundaryError')\n return\n console.error('Error in renderToPipeableStream:', error, info)\n },\n })\n } catch (e) {\n console.error('Error in renderToPipeableStream:', e)\n }\n\n const responseStream = transformPipeableStreamWithRouter(\n router,\n reactAppPassthrough,\n )\n return new Response(responseStream as any, {\n status: router.state.statusCode,\n headers: responseHeaders,\n })\n }\n\n throw new Error(\n 'No renderToReadableStream or renderToPipeableStream found in react-dom/server. Ensure you are using a version of react-dom that supports streaming.',\n )\n}\n"],"names":["isbot","transformReadableStreamWithRouter","PassThrough","transformPipeableStreamWithRouter"],"mappings":";;;;;;AAWO,MAAM,uBAAuB,OAAO;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAKM;AACA,MAAA,OAAO,eAAe,2BAA2B,YAAY;AAC/D,UAAM,SAAS,MAAM,eAAe,uBAAuB,UAAU;AAAA,MACnE,QAAQ,QAAQ;AAAA,IAAA,CACjB;AAED,QAAIA,MAAAA,MAAM,QAAQ,QAAQ,IAAI,YAAY,CAAC,GAAG;AAC5C,YAAM,OAAO;AAAA,IAAA;AAGf,UAAM,iBAAiBC,OAAA;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AACO,WAAA,IAAI,SAAS,gBAAuB;AAAA,MACzC,QAAQ,OAAO,MAAM;AAAA,MACrB,SAAS;AAAA,IAAA,CACV;AAAA,EAAA;AAGC,MAAA,OAAO,eAAe,2BAA2B,YAAY;AACzD,UAAA,sBAAsB,IAAIC,wBAAY;AAExC,QAAA;AACI,YAAA,WAAW,eAAe,uBAAuB,UAAU;AAAA,QAC/D,GAAIF,MAAAA,MAAM,QAAQ,QAAQ,IAAI,YAAY,CAAC,IACvC;AAAA,UACE,aAAa;AACX,qBAAS,KAAK,mBAAmB;AAAA,UAAA;AAAA,QACnC,IAEF;AAAA,UACE,eAAe;AACb,qBAAS,KAAK,mBAAmB;AAAA,UAAA;AAAA,QAErC;AAAA,QACJ,SAAS,CAAC,OAAO,SAAS;AACpB,cAAA,iBAAiB,SAAS,MAAM,YAAY;AAC9C;AACM,kBAAA,MAAM,oCAAoC,OAAO,IAAI;AAAA,QAAA;AAAA,MAC/D,CACD;AAAA,aACM,GAAG;AACF,cAAA,MAAM,oCAAoC,CAAC;AAAA,IAAA;AAGrD,UAAM,iBAAiBG,OAAA;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AACO,WAAA,IAAI,SAAS,gBAAuB;AAAA,MACzC,QAAQ,OAAO,MAAM;AAAA,MACrB,SAAS;AAAA,IAAA,CACV;AAAA,EAAA;AAGH,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;;"}