|
| 1 | +import * as Sentry from '@sentry/remix'; |
| 2 | + |
| 3 | +import { PassThrough } from 'node:stream'; |
| 4 | +import * as isbotModule from 'isbot'; |
| 5 | + |
| 6 | +import type { AppLoadContext, EntryContext } from '@remix-run/node'; |
| 7 | +import { createReadableStreamFromReadable } from '@remix-run/node'; |
| 8 | +import { installGlobals } from '@remix-run/node'; |
| 9 | +import { RemixServer } from '@remix-run/react'; |
| 10 | +import { renderToPipeableStream } from 'react-dom/server'; |
| 11 | + |
| 12 | +installGlobals(); |
| 13 | + |
| 14 | +const ABORT_DELAY = 5_000; |
| 15 | + |
| 16 | +export const handleError = Sentry.wrapRemixHandleError; |
| 17 | + |
| 18 | +export default function handleRequest( |
| 19 | + request: Request, |
| 20 | + responseStatusCode: number, |
| 21 | + responseHeaders: Headers, |
| 22 | + remixContext: EntryContext, |
| 23 | + loadContext: AppLoadContext, |
| 24 | +) { |
| 25 | + return isBotRequest(request.headers.get('user-agent')) |
| 26 | + ? handleBotRequest(request, responseStatusCode, responseHeaders, remixContext) |
| 27 | + : handleBrowserRequest(request, responseStatusCode, responseHeaders, remixContext); |
| 28 | +} |
| 29 | + |
| 30 | +// We have some Remix apps in the wild already running with isbot@3 so we need |
| 31 | +// to maintain backwards compatibility even though we want new apps to use |
| 32 | +// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev. |
| 33 | +function isBotRequest(userAgent: string | null) { |
| 34 | + if (!userAgent) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + // isbot >= 3.8.0, >4 |
| 39 | + if ('isbot' in isbotModule && typeof isbotModule.isbot === 'function') { |
| 40 | + return isbotModule.isbot(userAgent); |
| 41 | + } |
| 42 | + |
| 43 | + // isbot < 3.8.0 |
| 44 | + if ('default' in isbotModule && typeof isbotModule.default === 'function') { |
| 45 | + return isbotModule.default(userAgent); |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +function handleBotRequest( |
| 52 | + request: Request, |
| 53 | + responseStatusCode: number, |
| 54 | + responseHeaders: Headers, |
| 55 | + remixContext: EntryContext, |
| 56 | +) { |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + let shellRendered = false; |
| 59 | + const { pipe, abort } = renderToPipeableStream( |
| 60 | + <RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />, |
| 61 | + { |
| 62 | + onAllReady() { |
| 63 | + shellRendered = true; |
| 64 | + const body = new PassThrough(); |
| 65 | + const stream = createReadableStreamFromReadable(body); |
| 66 | + |
| 67 | + responseHeaders.set('Content-Type', 'text/html'); |
| 68 | + |
| 69 | + resolve( |
| 70 | + new Response(stream, { |
| 71 | + headers: responseHeaders, |
| 72 | + status: responseStatusCode, |
| 73 | + }), |
| 74 | + ); |
| 75 | + |
| 76 | + pipe(body); |
| 77 | + }, |
| 78 | + onShellError(error: unknown) { |
| 79 | + reject(error); |
| 80 | + }, |
| 81 | + onError(error: unknown) { |
| 82 | + responseStatusCode = 500; |
| 83 | + // Log streaming rendering errors from inside the shell. Don't log |
| 84 | + // errors encountered during initial shell rendering since they'll |
| 85 | + // reject and get logged in handleDocumentRequest. |
| 86 | + if (shellRendered) { |
| 87 | + console.error(error); |
| 88 | + } |
| 89 | + }, |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + setTimeout(abort, ABORT_DELAY); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +function handleBrowserRequest( |
| 98 | + request: Request, |
| 99 | + responseStatusCode: number, |
| 100 | + responseHeaders: Headers, |
| 101 | + remixContext: EntryContext, |
| 102 | +) { |
| 103 | + return new Promise((resolve, reject) => { |
| 104 | + let shellRendered = false; |
| 105 | + const { pipe, abort } = renderToPipeableStream( |
| 106 | + <RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />, |
| 107 | + { |
| 108 | + onShellReady() { |
| 109 | + shellRendered = true; |
| 110 | + const body = new PassThrough(); |
| 111 | + const stream = createReadableStreamFromReadable(body); |
| 112 | + |
| 113 | + responseHeaders.set('Content-Type', 'text/html'); |
| 114 | + |
| 115 | + resolve( |
| 116 | + new Response(stream, { |
| 117 | + headers: responseHeaders, |
| 118 | + status: responseStatusCode, |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + pipe(body); |
| 123 | + }, |
| 124 | + onShellError(error: unknown) { |
| 125 | + reject(error); |
| 126 | + }, |
| 127 | + onError(error: unknown) { |
| 128 | + responseStatusCode = 500; |
| 129 | + // Log streaming rendering errors from inside the shell. Don't log |
| 130 | + // errors encountered during initial shell rendering since they'll |
| 131 | + // reject and get logged in handleDocumentRequest. |
| 132 | + if (shellRendered) { |
| 133 | + console.error(error); |
| 134 | + } |
| 135 | + }, |
| 136 | + }, |
| 137 | + ); |
| 138 | + |
| 139 | + setTimeout(abort, ABORT_DELAY); |
| 140 | + }); |
| 141 | +} |
0 commit comments