UNPKG

@tanstack/react-router

Version:

Modern and scalable routing for React applications

1 lines 4.43 kB
{"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 { AnyRouter } from '@tanstack/router-core'\nimport type { ReadableStream } from 'node:stream/web'\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 nonce: router.options.ssr?.nonce,\n progressiveChunkSize: Number.POSITIVE_INFINITY,\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 nonce: router.options.ssr?.nonce,\n progressiveChunkSize: Number.POSITIVE_INFINITY,\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 console.error('Error in renderToPipeableStream:', error, info)\n // Destroy the passthrough stream on error\n if (!reactAppPassthrough.destroyed) {\n reactAppPassthrough.destroy(\n error instanceof Error ? error : new Error(String(error)),\n )\n }\n },\n })\n } catch (e) {\n console.error('Error in renderToPipeableStream:', e)\n reactAppPassthrough.destroy(e instanceof Error ? e : new Error(String(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;AACJ,MAAI,OAAO,eAAe,2BAA2B,YAAY;AAC/D,UAAM,SAAS,MAAM,eAAe,uBAAuB,UAAU;AAAA,MACnE,QAAQ,QAAQ;AAAA,MAChB,OAAO,OAAO,QAAQ,KAAK;AAAA,MAC3B,sBAAsB,OAAO;AAAA,IAAA,CAC9B;AAED,QAAIA,MAAAA,MAAM,QAAQ,QAAQ,IAAI,YAAY,CAAC,GAAG;AAC5C,YAAM,OAAO;AAAA,IACf;AAEA,UAAM,iBAAiBC,OAAAA;AAAAA,MACrB;AAAA,MACA;AAAA,IAAA;AAEF,WAAO,IAAI,SAAS,gBAAuB;AAAA,MACzC,QAAQ,OAAO,MAAM;AAAA,MACrB,SAAS;AAAA,IAAA,CACV;AAAA,EACH;AAEA,MAAI,OAAO,eAAe,2BAA2B,YAAY;AAC/D,UAAM,sBAAsB,IAAIC,wBAAA;AAEhC,QAAI;AACF,YAAM,WAAW,eAAe,uBAAuB,UAAU;AAAA,QAC/D,OAAO,OAAO,QAAQ,KAAK;AAAA,QAC3B,sBAAsB,OAAO;AAAA,QAC7B,GAAIF,MAAAA,MAAM,QAAQ,QAAQ,IAAI,YAAY,CAAC,IACvC;AAAA,UACE,aAAa;AACX,qBAAS,KAAK,mBAAmB;AAAA,UACnC;AAAA,QAAA,IAEF;AAAA,UACE,eAAe;AACb,qBAAS,KAAK,mBAAmB;AAAA,U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