90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
|
/**
|
||
|
* By default, Remix will handle generating the HTTP Response for you.
|
||
|
* 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` ✨
|
||
|
* For more information, see https://remix.run/file-conventions/entry.server
|
||
|
*/
|
||
|
|
||
|
import { PassThrough } from "node:stream";
|
||
|
|
||
|
import type { AppLoadContext, EntryContext } from "@remix-run/node";
|
||
|
import { createReadableStreamFromReadable } from "@remix-run/node";
|
||
|
import { RemixServer } from "@remix-run/react";
|
||
|
import { isbot } from "isbot";
|
||
|
import { renderToPipeableStream } from "react-dom/server";
|
||
|
|
||
|
const ABORT_DELAY = 5_000;
|
||
|
|
||
|
export default function handleRequest(
|
||
|
request: Request,
|
||
|
responseStatusCode: number,
|
||
|
responseHeaders: Headers,
|
||
|
remixContext: EntryContext,
|
||
|
loadContext: AppLoadContext
|
||
|
) {
|
||
|
return isbot(request.headers.get("user-agent") || "")
|
||
|
? handleBotRequest(request, responseStatusCode, responseHeaders, remixContext)
|
||
|
: handleBrowserRequest(request, responseStatusCode, responseHeaders, remixContext);
|
||
|
}
|
||
|
|
||
|
function handleBotRequest(
|
||
|
request: Request,
|
||
|
responseStatusCode: number,
|
||
|
responseHeaders: Headers,
|
||
|
remixContext: EntryContext
|
||
|
) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const { pipe, abort } = renderToPipeableStream(
|
||
|
<RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />,
|
||
|
{
|
||
|
onAllReady() {
|
||
|
const body = new PassThrough();
|
||
|
const stream = createReadableStreamFromReadable(body);
|
||
|
responseHeaders.set("Content-Type", "text/html");
|
||
|
resolve(
|
||
|
new Response(stream, {
|
||
|
headers: responseHeaders,
|
||
|
status: responseStatusCode,
|
||
|
})
|
||
|
);
|
||
|
pipe(body);
|
||
|
},
|
||
|
onShellError(error: unknown) {
|
||
|
reject(error);
|
||
|
},
|
||
|
}
|
||
|
);
|
||
|
setTimeout(abort, ABORT_DELAY);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function handleBrowserRequest(
|
||
|
request: Request,
|
||
|
responseStatusCode: number,
|
||
|
responseHeaders: Headers,
|
||
|
remixContext: EntryContext
|
||
|
) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const { pipe, abort } = renderToPipeableStream(
|
||
|
<RemixServer context={remixContext} url={request.url} abortDelay={ABORT_DELAY} />,
|
||
|
{
|
||
|
onShellReady() {
|
||
|
const body = new PassThrough();
|
||
|
const stream = createReadableStreamFromReadable(body);
|
||
|
responseHeaders.set("Content-Type", "text/html");
|
||
|
resolve(
|
||
|
new Response(stream, {
|
||
|
headers: responseHeaders,
|
||
|
status: responseStatusCode,
|
||
|
})
|
||
|
);
|
||
|
pipe(body);
|
||
|
},
|
||
|
onShellError(error: unknown) {
|
||
|
reject(error);
|
||
|
},
|
||
|
}
|
||
|
);
|
||
|
setTimeout(abort, ABORT_DELAY);
|
||
|
});
|
||
|
}
|