Skip to content

Commit

Permalink
workaround(node ssr): cancellation support for renderToAsyncIterable (#…
Browse files Browse the repository at this point in the history
…10319)

* workaround(node ssr): cancellation support for renderToAsyncIterable

* add changeset

* Update .changeset/nice-pets-tie.md
  • Loading branch information
lilnasy authored Mar 4, 2024
1 parent 9076dc8 commit 19eccce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit 19eccce

Please sign in to comment.