|
| 1 | +/** |
| 2 | + * By default, Remix will handle generating the HTTP Response for you. |
| 3 | + * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨ |
| 4 | + * For more information, see https://remix.run/file-conventions/entry.server |
| 5 | + */ |
| 6 | + |
| 7 | +import type { AppLoadContext, EntryContext } from '@remix-run/cloudflare' |
| 8 | +import { RemixServer } from '@remix-run/react' |
| 9 | +import { isbot } from 'isbot' |
| 10 | +import { renderToReadableStream } from 'react-dom/server' |
| 11 | + |
| 12 | +const ABORT_DELAY = 5000 |
| 13 | + |
| 14 | +export default async function handleRequest( |
| 15 | + request: Request, |
| 16 | + responseStatusCode: number, |
| 17 | + responseHeaders: Headers, |
| 18 | + remixContext: EntryContext, |
| 19 | + // This is ignored so we can keep it in the template for visibility. Feel |
| 20 | + // free to delete this parameter in your app if you're not using it! |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 22 | + loadContext: AppLoadContext |
| 23 | +) { |
| 24 | + const controller = new AbortController() |
| 25 | + const timeoutId = setTimeout(() => controller.abort(), ABORT_DELAY) |
| 26 | + |
| 27 | + const body = await renderToReadableStream( |
| 28 | + <RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />, |
| 29 | + { |
| 30 | + signal: controller.signal, |
| 31 | + onError(error: unknown) { |
| 32 | + if (!controller.signal.aborted) { |
| 33 | + // Log streaming rendering errors from inside the shell |
| 34 | + console.error(error) |
| 35 | + } |
| 36 | + responseStatusCode = 500 |
| 37 | + }, |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + body.allReady.then(() => clearTimeout(timeoutId)) |
| 42 | + |
| 43 | + if (isbot(request.headers.get('user-agent') || '')) { |
| 44 | + await body.allReady |
| 45 | + } |
| 46 | + |
| 47 | + responseHeaders.set('Content-Type', 'text/html') |
| 48 | + return new Response(body, { |
| 49 | + headers: responseHeaders, |
| 50 | + status: responseStatusCode, |
| 51 | + }) |
| 52 | +} |
0 commit comments