Skip to content

Commit

Permalink
fix(turborepo-vercel): remove streaming (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored and mcansh committed Dec 16, 2022
1 parent c2a9ffe commit 4452dfb
Showing 1 changed file with 11 additions and 100 deletions.
111 changes: 11 additions & 100 deletions turborepo-vercel/apps/remix-app/app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,110 +1,21 @@
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;

const handleRequest = (
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) =>
isbot(request.headers.get("user-agent"))
? handleBotRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
)
: handleBrowserRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
);
export default handleRequest;
import { renderToString } from "react-dom/server";

const handleBotRequest = (
export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) =>
new Promise((resolve, reject) => {
let didError = false;

const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} />,
{
onAllReady: () => {
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
);

pipe(body);
},
onShellError: (error: unknown) => {
reject(error);
},
onError: (error: unknown) => {
didError = true;

console.error(error);
},
}
);

setTimeout(abort, ABORT_DELAY);
});

const handleBrowserRequest = (
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) =>
new Promise((resolve, reject) => {
let didError = false;

const { pipe, abort } = renderToPipeableStream(
<RemixServer context={remixContext} url={request.url} />,
{
onShellReady: () => {
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
headers: responseHeaders,
status: didError ? 500 : responseStatusCode,
})
);

pipe(body);
},
onShellError: (error: unknown) => {
reject(error);
},
onError: (error: unknown) => {
didError = true;
) {
const markup = renderToString(
<RemixServer context={remixContext} url={request.url} />
);

console.error(error);
},
}
);
responseHeaders.set("Content-Type", "text/html");

setTimeout(abort, ABORT_DELAY);
return new Response("<!DOCTYPE html>" + markup, {
headers: responseHeaders,
status: responseStatusCode,
});
}

0 comments on commit 4452dfb

Please sign in to comment.