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

renderToPipeableStream: Expose errors through onError, no longer emit un-catchable error event on internal stream #405

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/mighty-keys-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

renderToPipeableStream: expose errors through onError, no longer emit un-catchable error event on internal stream
2 changes: 1 addition & 1 deletion src/stream-node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface RenderToPipeableStreamOptions {
}

interface PipeableStream {
abort: () => void;
abort: (reason?: unknown) => void;
pipe: (writable: WritableStream) => void;
}

Expand Down
17 changes: 14 additions & 3 deletions src/stream-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,28 @@ export function renderToPipeableStream(vnode, options, context) {
stream.end();
})
.catch((error) => {
stream.destroy(error);
stream.destroy();
if (options.onError) {
options.onError(error);
} else {
throw error;
}
});

Promise.resolve().then(() => {
options.onShellReady && options.onShellReady();
});

return {
abort() {
/**
* @param {unknown} [reason]
*/
abort(reason = new Error('The render was aborted by the server without a reason.')) {
controller.abort();
stream.destroy(new Error('aborted'));
stream.destroy();
if (options.onError) {
options.onError(reason);
}
},
/**
* @param {import("stream").Writable} writable
Expand Down
Loading