From d2e5b2c1f4fe5f84e727b02c972307c120331af0 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Thu, 6 Feb 2025 22:09:47 -0600 Subject: [PATCH 1/2] fix: Only abort pipeablestream if not complete --- src/stream-node.js | 9 ++++++++- test/compat/stream-node.test.js | 15 +++++++++++++++ test/compat/stream.test.js | 4 ++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/stream-node.js b/src/stream-node.js index bffa20e..e39da31 100644 --- a/src/stream-node.js +++ b/src/stream-node.js @@ -60,7 +60,14 @@ export function renderToPipeableStream(vnode, options, context) { /** * @param {unknown} [reason] */ - abort(reason = new Error('The render was aborted by the server without a reason.')) { + abort( + reason = new Error( + 'The render was aborted by the server without a reason.' + ) + ) { + // Remix/React-Router will always call abort after a timeout, even on success + if (stream.closed) return; + controller.abort(); stream.destroy(); if (options.onError) { diff --git a/test/compat/stream-node.test.js b/test/compat/stream-node.test.js index 5783887..159e0e2 100644 --- a/test/compat/stream-node.test.js +++ b/test/compat/stream-node.test.js @@ -73,4 +73,19 @@ describe('renderToPipeableStream', () => { '' ]); }); + + it('should not error if the stream has already been closed', async () => { + let error; + const sink = createSink(); + const { pipe, abort } = renderToPipeableStream(
bar
, { + onAllReady: () => { + pipe(sink.stream); + }, + onError: (e) => (error = e) + }); + await sink.promise; + abort(); + + expect(error).to.be.undefined; + }); }); diff --git a/test/compat/stream.test.js b/test/compat/stream.test.js index 09f6134..f2378a6 100644 --- a/test/compat/stream.test.js +++ b/test/compat/stream.test.js @@ -82,10 +82,10 @@ describe('renderToReadableStream', () => { const result = await sink.promise; expect(result).to.deep.equal([ - '
loading...
', + '
loading...
', '' ]); }); From cdfae8693d8dcd4d8a480186c39f39bb49bb4372 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Thu, 6 Feb 2025 22:16:30 -0600 Subject: [PATCH 2/2] docs: Add changeset --- .changeset/yellow-hounds-jog.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/yellow-hounds-jog.md diff --git a/.changeset/yellow-hounds-jog.md b/.changeset/yellow-hounds-jog.md new file mode 100644 index 0000000..2b3b40a --- /dev/null +++ b/.changeset/yellow-hounds-jog.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +Only abort/report errors from `renderToPipeableStream()` if the stream hasn't already been closed