mirror of
https://github.com/tubearchivist/tubearchivist-frontend.git
synced 2024-11-22 03:40:14 +00:00
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { PassThrough } from "stream";
|
|
import type { EntryContext } from "@remix-run/node";
|
|
import { Response } from "@remix-run/node";
|
|
import { RemixServer } from "@remix-run/react";
|
|
import isbot from "isbot";
|
|
import { renderToPipeableStream } from "react-dom/server";
|
|
|
|
const ABORT_DELAY = 5000;
|
|
|
|
export default function handleRequest(
|
|
request: Request,
|
|
responseStatusCode: number,
|
|
responseHeaders: Headers,
|
|
remixContext: EntryContext
|
|
) {
|
|
const callbackName = isbot(request.headers.get("user-agent"))
|
|
? "onAllReady"
|
|
: "onShellReady";
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let didError = false;
|
|
|
|
const { pipe, abort } = renderToPipeableStream(
|
|
<RemixServer context={remixContext} url={request.url} />,
|
|
{
|
|
[callbackName]: () => {
|
|
const body = new PassThrough();
|
|
|
|
responseHeaders.set("Content-Type", "text/html");
|
|
|
|
resolve(
|
|
new Response(body, {
|
|
headers: responseHeaders,
|
|
status: didError ? 500 : responseStatusCode,
|
|
})
|
|
);
|
|
|
|
pipe(body);
|
|
},
|
|
onShellError: (err: unknown) => {
|
|
reject(err);
|
|
},
|
|
onError: (error: unknown) => {
|
|
didError = true;
|
|
|
|
console.error(error);
|
|
},
|
|
}
|
|
);
|
|
|
|
setTimeout(abort, ABORT_DELAY);
|
|
});
|
|
}
|