Created
September 7, 2024 06:27
-
-
Save yusukebe/48f4d2ec243405d6aee05d22d8b7fdc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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