-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallelize rendering of sibling components to avoid async waterfalls (…
…#7071) * Parallelize rendering of sibling components to avoid async waterfalls * Catch and rethrow errors when eagerly rendering children * Catch `Response` in rendering stage and throw error * Add changeset * Fix test error message * Improve unit tests * Start async generators in non-buffered mode, and only start buffering once a component doesn't resolve immediatly * Add more documentation
- Loading branch information
Johannes Spohr
authored
May 18, 2023
1 parent
05695ab
commit e186ecc
Showing
10 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Render sibling components in parallel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@test/parallel-components", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "astro build", | ||
"dev": "astro dev" | ||
}, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/parallel/src/components/Delayed.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
const { ms } = Astro.props | ||
const start = new Date().valueOf(); | ||
await new Promise(res => setTimeout(res, ms)); | ||
const finished = new Date().valueOf(); | ||
--- | ||
<section> | ||
<h1>{ms}ms Delayed</h1> | ||
<span class="start">{ start }</span> | ||
<span class="finished">{ finished }</span> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
import Delayed from '../components/Delayed.astro' | ||
--- | ||
|
||
<Delayed ms={30} /> | ||
<Delayed ms={20} /> | ||
<Delayed ms={40} /> | ||
<Delayed ms={10} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Component parallelization', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/parallel/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('renders fast', async () => { | ||
let html = await fixture.readFile('/index.html'); | ||
let $ = cheerio.load(html); | ||
|
||
const startTimes = Array.from($('.start')).map((element) => Number(element.children[0].data)); | ||
const finishTimes = Array.from($('.finished')).map((element) => | ||
Number(element.children[0].data) | ||
); | ||
|
||
let renderStartWithin = Math.max(...startTimes) - Math.min(...startTimes); | ||
expect(renderStartWithin).to.be.lessThan( | ||
10, // in theory, this should be 0, so 10ms tolerance | ||
"The components didn't start rendering in parallel" | ||
); | ||
|
||
const totalRenderTime = Math.max(...finishTimes) - Math.min(...startTimes); | ||
expect(totalRenderTime).to.be.lessThan( | ||
60, // max component delay is 40ms | ||
'The total render time was significantly longer than the max component delay' | ||
); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.