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

workaround(node ssr): cancellation support for renderToAsyncIterable #10319

Merged
merged 3 commits into from
Mar 4, 2024
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/nice-pets-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where streaming SSR responses sometimes failed with "`iterator.result` is not a function" on node-based adapters.
10 changes: 9 additions & 1 deletion packages/astro/src/runtime/server/render/astro/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,14 @@ export async function renderToAsyncIterable(
// The `next` is an object `{ promise, resolve, reject }` that we use to wait
// for chunks to be pushed into the buffer.
let next = promiseWithResolvers<void>();
// keep track of whether the client connection is still interested in the response.
let cancelled = false;
const buffer: Uint8Array[] = []; // []Uint8Array

const iterator = {
const iterator: AsyncIterator<Uint8Array> = {
async next() {
if (cancelled) return { done: true, value: undefined };

await next.promise;

// If an error occurs during rendering, throw the error as we cannot proceed.
Expand Down Expand Up @@ -238,6 +242,10 @@ export async function renderToAsyncIterable(

return returnValue;
},
async return() {
cancelled = true;
return { done: true, value: undefined };
}
};

const destination: RenderDestination = {
Expand Down
Loading