|
| 1 | +import React from 'react'; |
| 2 | +import type { AppLoadContext, EntryContext, ServerRouter } from 'react-router'; |
| 3 | +import type { ReactNode } from 'react'; |
| 4 | +import { getMetaTagTransformer, wrapSentryHandleRequest } from './wrapSentryHandleRequest'; |
| 5 | +import type { createReadableStreamFromReadable } from '@react-router/node'; |
| 6 | +import { PassThrough } from 'stream'; |
| 7 | + |
| 8 | +type RenderToPipeableStreamOptions = { |
| 9 | + [key: string]: unknown; |
| 10 | + onShellReady?: () => void; |
| 11 | + onAllReady?: () => void; |
| 12 | + onShellError?: (error: unknown) => void; |
| 13 | + onError?: (error: unknown) => void; |
| 14 | +}; |
| 15 | + |
| 16 | +type RenderToPipeableStreamResult = { |
| 17 | + pipe: (destination: NodeJS.WritableStream) => void; |
| 18 | + abort: () => void; |
| 19 | +}; |
| 20 | + |
| 21 | +type RenderToPipeableStreamFunction = ( |
| 22 | + node: ReactNode, |
| 23 | + options: RenderToPipeableStreamOptions, |
| 24 | +) => RenderToPipeableStreamResult; |
| 25 | + |
| 26 | +export interface SentryHandleRequestOptions { |
| 27 | + /** |
| 28 | + * Timeout in milliseconds after which the rendering stream will be aborted |
| 29 | + * @default 10000 |
| 30 | + */ |
| 31 | + streamTimeout?: number; |
| 32 | + |
| 33 | + /** |
| 34 | + * React's renderToPipeableStream function from 'react-dom/server' |
| 35 | + */ |
| 36 | + renderToPipeableStream: RenderToPipeableStreamFunction; |
| 37 | + |
| 38 | + /** |
| 39 | + * The <ServerRouter /> component from '@react-router/server' |
| 40 | + */ |
| 41 | + ServerRouter: typeof ServerRouter; |
| 42 | + |
| 43 | + /** |
| 44 | + * createReadableStreamFromReadable from '@react-router/node' |
| 45 | + */ |
| 46 | + createReadableStreamFromReadable: typeof createReadableStreamFromReadable; |
| 47 | + |
| 48 | + /** |
| 49 | + * Regular expression to identify bot user agents |
| 50 | + * @default /bot|crawler|spider|googlebot|chrome-lighthouse|baidu|bing|google|yahoo|lighthouse/i |
| 51 | + */ |
| 52 | + botRegex?: RegExp; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * A complete Sentry-instrumented handleRequest implementation that handles both |
| 57 | + * route parametrization and trace meta tag injection. |
| 58 | + * |
| 59 | + * @param options Configuration options |
| 60 | + * @returns A Sentry-instrumented handleRequest function |
| 61 | + */ |
| 62 | +export function createSentryHandleRequest( |
| 63 | + options: SentryHandleRequestOptions, |
| 64 | +): ( |
| 65 | + request: Request, |
| 66 | + responseStatusCode: number, |
| 67 | + responseHeaders: Headers, |
| 68 | + routerContext: EntryContext, |
| 69 | + loadContext: AppLoadContext, |
| 70 | +) => Promise<unknown> { |
| 71 | + const { |
| 72 | + streamTimeout = 10000, |
| 73 | + renderToPipeableStream, |
| 74 | + ServerRouter, |
| 75 | + createReadableStreamFromReadable, |
| 76 | + botRegex = /bot|crawler|spider|googlebot|chrome-lighthouse|baidu|bing|google|yahoo|lighthouse/i, |
| 77 | + } = options; |
| 78 | + |
| 79 | + const handleRequest = function handleRequest( |
| 80 | + request: Request, |
| 81 | + responseStatusCode: number, |
| 82 | + responseHeaders: Headers, |
| 83 | + routerContext: EntryContext, |
| 84 | + _loadContext: AppLoadContext, |
| 85 | + ): Promise<Response> { |
| 86 | + return new Promise((resolve, reject) => { |
| 87 | + let shellRendered = false; |
| 88 | + const userAgent = request.headers.get('user-agent'); |
| 89 | + |
| 90 | + // Determine if we should use onAllReady or onShellReady |
| 91 | + const isBot = typeof userAgent === 'string' && botRegex.test(userAgent); |
| 92 | + const isSpaMode = !!(routerContext as { isSpaMode?: boolean }).isSpaMode; |
| 93 | + |
| 94 | + const readyOption = isBot || isSpaMode ? 'onAllReady' : 'onShellReady'; |
| 95 | + |
| 96 | + const { pipe, abort } = renderToPipeableStream(<ServerRouter context={routerContext} url={request.url} />, { |
| 97 | + [readyOption]() { |
| 98 | + shellRendered = true; |
| 99 | + const body = new PassThrough(); |
| 100 | + |
| 101 | + const stream = createReadableStreamFromReadable(body); |
| 102 | + |
| 103 | + responseHeaders.set('Content-Type', 'text/html'); |
| 104 | + |
| 105 | + resolve( |
| 106 | + new Response(stream, { |
| 107 | + headers: responseHeaders, |
| 108 | + status: responseStatusCode, |
| 109 | + }), |
| 110 | + ); |
| 111 | + |
| 112 | + // this injects trace data to the HTML head |
| 113 | + pipe(getMetaTagTransformer(body)); |
| 114 | + }, |
| 115 | + onShellError(error: unknown) { |
| 116 | + reject(error); |
| 117 | + }, |
| 118 | + onError(error: unknown) { |
| 119 | + // eslint-disable-next-line no-param-reassign |
| 120 | + responseStatusCode = 500; |
| 121 | + // Log streaming rendering errors from inside the shell. Don't log |
| 122 | + // errors encountered during initial shell rendering since they'll |
| 123 | + // reject and get logged in handleDocumentRequest. |
| 124 | + if (shellRendered) { |
| 125 | + // eslint-disable-next-line no-console |
| 126 | + console.error(error); |
| 127 | + } |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + // Abort the rendering stream after the `streamTimeout` |
| 132 | + setTimeout(abort, streamTimeout); |
| 133 | + }); |
| 134 | + }; |
| 135 | + |
| 136 | + // Wrap the handle request function for request parametrization |
| 137 | + return wrapSentryHandleRequest(handleRequest); |
| 138 | +} |
0 commit comments