Skip to content

Commit

Permalink
Close iterator after rendering is complete and no more chunks remain (#…
Browse files Browse the repository at this point in the history
…11210)

* Close iterator after rendering is complete and no more chunks remain

* Wait for the first whole render before resolving
  • Loading branch information
matthewp committed Jun 10, 2024
1 parent 2e6d0d9 commit 66fc028
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-pianos-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Close the iterator only after rendering is complete
12 changes: 10 additions & 2 deletions packages/astro/src/runtime/server/render/astro/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ export async function renderToAsyncIterable(
if (next !== null) {
await next.promise;
}
// Buffer is empty so there's nothing to receive, wait for the next resolve.
else if(!renderingComplete && !buffer.length) {
next = promiseWithResolvers();
await next.promise;
}

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

const returnValue = {
// The iterator is done if there are no chunks to return.
done: length === 0,
// The iterator is done when rendering has finished
// and there are no more chunks to return.
done: length === 0 && renderingComplete,
value: mergedArray,
};

Expand Down Expand Up @@ -305,6 +311,8 @@ export async function renderToAsyncIterable(
// will run.
buffer.push(bytes);
next?.resolve();
} else if(buffer.length > 0) {
next?.resolve();
}
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
export const partial = true
---

{
true && (
<>
{true && <div id="true">test</div>}
{false && <div>test</div>}
</>
)
}
7 changes: 7 additions & 0 deletions packages/astro/test/partials.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Partials', () => {
Expand Down Expand Up @@ -28,6 +29,12 @@ describe('Partials', () => {
const html = await fixture.fetch('/partials/item/').then((res) => res.text());
assert.equal(html.startsWith('<li'), true);
});

it('Nested conditionals render', async () => {
const html = await fixture.fetch('/partials/nested-conditional/').then((res) => res.text());
const $ = cheerio.load(html);
assert.equal($('#true').text(), 'test');
});
});

describe('build', () => {
Expand Down

0 comments on commit 66fc028

Please sign in to comment.