Skip to content

Commit

Permalink
fix(@netlify/remix-edge-adapter): don't close the stream twice (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Sep 5, 2024
1 parent 2c5833c commit 07bef21
Showing 1 changed file with 21 additions and 2 deletions.
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

0 comments on commit 07bef21

Please sign in to comment.