Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@netlify/remix-edge-adapter): don't close the stream twice #444

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/remix-edge-adapter/src/common/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,42 @@ export default async function handleRequest(
remixContext: EntryContext,
_loadContext: AppLoadContext,
) {
let isStreamClosing = false

const abortController = new AbortController()
request.signal.addEventListener('abort', () => {
if (!isStreamClosing) {
// only signal the abort if the stream is not already closing
abortController.abort(request.signal.reason)
}
})

// The main difference between this and the default Node.js entrypoint is
// this use of web streams as opposed to Node.js streams.
const body = await ReactDOMServer.renderToReadableStream(<RemixServer context={remixContext} url={request.url} />, {
signal: request.signal,
signal: abortController.signal,
onError(error: unknown) {
// Log streaming rendering errors from inside the shell
console.error(error)
responseStatusCode = 500
},
})

// identity transform just to be able to listen for the flush event
const transformedBody = body.pipeThrough(
new TransformStream({
flush() {
isStreamClosing = true
},
}),
)

if (isbot(request.headers.get('user-agent') || '')) {
await body.allReady
}

responseHeaders.set('Content-Type', 'text/html')
return new Response(body, {
return new Response(transformedBody, {
headers: responseHeaders,
status: responseStatusCode,
})
Expand Down