Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created September 7, 2024 06:27
Show Gist options
  • Save yusukebe/48f4d2ec243405d6aee05d22d8b7fdc6 to your computer and use it in GitHub Desktop.
Save yusukebe/48f4d2ec243405d6aee05d22d8b7fdc6 to your computer and use it in GitHub Desktop.
import type { EntryContext } from "@remix-run/cloudflare";
import { renderToReadableStream } from "react-dom/server";
import { Hono } from "hono";
import { RemixServer } from "@remix-run/react";
export const handle =
(userApp?: Hono) =>
(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) => {
const app = new Hono<{
Bindings: {
responseStatusCode: number;
responseHeaders: Headers;
remixContext: EntryContext;
};
}>();
if (userApp) {
app.route("/", userApp);
}
app.all("*", async (c) => {
const request = c.req.raw;
const body = await renderToReadableStream(
<RemixServer context={c.env.remixContext} url={request.url} />,
{
signal: request.signal,
onError(error: unknown) {
console.error(error);
c.env.responseStatusCode = 500;
},
}
);
c.header("Content-Type", "text/html");
return c.body(body, {
status: c.env.responseStatusCode,
headers: c.env.responseHeaders,
});
});
return app.fetch(request, {
remixContext,
responseStatusCode,
responseHeaders,
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment