Skip to content

Commit 66fc028

Browse files
authoredJun 10, 2024··
Close iterator after rendering is complete and no more chunks remain (#11210)
* Close iterator after rendering is complete and no more chunks remain * Wait for the first whole render before resolving
1 parent 2e6d0d9 commit 66fc028

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed
 

‎.changeset/purple-pianos-greet.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Close the iterator only after rendering is complete

‎packages/astro/src/runtime/server/render/astro/render.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ export async function renderToAsyncIterable(
239239
if (next !== null) {
240240
await next.promise;
241241
}
242+
// Buffer is empty so there's nothing to receive, wait for the next resolve.
243+
else if(!renderingComplete && !buffer.length) {
244+
next = promiseWithResolvers();
245+
await next.promise;
246+
}
242247

243248
// Only create a new promise if rendering is still ongoing. Otherwise
244249
// there will be a dangling promises that breaks tests (probably not an actual app)
@@ -270,8 +275,9 @@ export async function renderToAsyncIterable(
270275
buffer.length = 0;
271276

272277
const returnValue = {
273-
// The iterator is done if there are no chunks to return.
274-
done: length === 0,
278+
// The iterator is done when rendering has finished
279+
// and there are no more chunks to return.
280+
done: length === 0 && renderingComplete,
275281
value: mergedArray,
276282
};
277283

@@ -305,6 +311,8 @@ export async function renderToAsyncIterable(
305311
// will run.
306312
buffer.push(bytes);
307313
next?.resolve();
314+
} else if(buffer.length > 0) {
315+
next?.resolve();
308316
}
309317
},
310318
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
export const partial = true
3+
---
4+
5+
{
6+
true && (
7+
<>
8+
{true && <div id="true">test</div>}
9+
{false && <div>test</div>}
10+
</>
11+
)
12+
}

‎packages/astro/test/partials.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'node:assert/strict';
22
import { after, before, describe, it } from 'node:test';
3+
import * as cheerio from 'cheerio';
34
import { loadFixture } from './test-utils.js';
45

56
describe('Partials', () => {
@@ -28,6 +29,12 @@ describe('Partials', () => {
2829
const html = await fixture.fetch('/partials/item/').then((res) => res.text());
2930
assert.equal(html.startsWith('<li'), true);
3031
});
32+
33+
it('Nested conditionals render', async () => {
34+
const html = await fixture.fetch('/partials/nested-conditional/').then((res) => res.text());
35+
const $ = cheerio.load(html);
36+
assert.equal($('#true').text(), 'test');
37+
});
3138
});
3239

3340
describe('build', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.