mirror of
https://github.com/stackblitz/bolt.new
synced 2024-11-27 14:32:46 +00:00
35 lines
1018 B
TypeScript
35 lines
1018 B
TypeScript
|
import type { AppLoadContext, EntryContext } from '@remix-run/cloudflare';
|
||
|
import { RemixServer } from '@remix-run/react';
|
||
|
import { isbot } from 'isbot';
|
||
|
import { renderToReadableStream } from 'react-dom/server';
|
||
|
|
||
|
export default async function handleRequest(
|
||
|
request: Request,
|
||
|
responseStatusCode: number,
|
||
|
responseHeaders: Headers,
|
||
|
remixContext: EntryContext,
|
||
|
_loadContext: AppLoadContext,
|
||
|
) {
|
||
|
const body = await renderToReadableStream(<RemixServer context={remixContext} url={request.url} />, {
|
||
|
signal: request.signal,
|
||
|
onError(error: unknown) {
|
||
|
console.error(error);
|
||
|
responseStatusCode = 500;
|
||
|
},
|
||
|
});
|
||
|
|
||
|
if (isbot(request.headers.get('user-agent') || '')) {
|
||
|
await body.allReady;
|
||
|
}
|
||
|
|
||
|
responseHeaders.set('Content-Type', 'text/html');
|
||
|
|
||
|
responseHeaders.set('Cross-Origin-Embedder-Policy', 'require-corp');
|
||
|
responseHeaders.set('Cross-Origin-Opener-Policy', 'same-origin');
|
||
|
|
||
|
return new Response(body, {
|
||
|
headers: responseHeaders,
|
||
|
status: responseStatusCode,
|
||
|
});
|
||
|
}
|