-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(astro): recover island hydration from transient import failures #16412
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
Merged
ematipico
merged 6 commits into
withastro:main
from
0xbejaxer:fix-16341-hydration-recovery
May 6, 2026
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8387b1e
fix(astro): recover and report island hydration import failures
0xbejaxer ff7eee5
chore: sync lockfile for hydration error fixture
0xbejaxer c91e1be
test(astro): wait for hydration in retry e2e assertion
0xbejaxer 1966afe
chore: trigger ci rerun after transient lint failure
0xbejaxer 622ad51
chore: rerun CI after flaky windows e2e failure
0xbejaxer 2ae3eec
Apply suggestion from @ematipico
ematipico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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': patch | ||
|
ematipico marked this conversation as resolved.
Outdated
|
||
| --- | ||
|
|
||
| Add retry and error event handling for `astro-island` hydration import failures to reduce unrecoverable hydration errors on transient network failures. | ||
This file contains hidden or 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,82 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { testFactory, waitForHydrate } from './test-utils.js'; | ||
|
|
||
| const test = testFactory(import.meta.url, { | ||
| root: './fixtures/astro-island-hydration-error/', | ||
| }); | ||
|
|
||
| test.describe('astro-island hydration error handling', () => { | ||
| let devServer; | ||
|
|
||
| test.beforeAll(async ({ astro }) => { | ||
| devServer = await astro.startDevServer(); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await devServer.stop(); | ||
| }); | ||
|
|
||
| test('recovers hydration after first failed component import', async ({ page, astro }) => { | ||
| const pageUrl = astro.resolveUrl('/'); | ||
| const html = await (await page.request.get(pageUrl)).text(); | ||
| const componentUrl = getIslandComponentUrl(html); | ||
|
|
||
| let attempts = 0; | ||
| await page.route(`**${componentUrl.split('?')[0]}*`, async (route) => { | ||
| attempts++; | ||
| if (attempts === 1) { | ||
| await route.abort('failed'); | ||
| return; | ||
| } | ||
| await route.continue(); | ||
| }); | ||
|
|
||
| await page.goto(pageUrl); | ||
| const incrementButton = page.locator('#counter .increment'); | ||
| const counter = page.locator('#counter'); | ||
| const count = counter.locator('pre'); | ||
| await waitForHydrate(page, counter); | ||
| await incrementButton.click(); | ||
| await expect(count).toHaveText('1'); | ||
| expect(attempts).toBe(2); | ||
| }); | ||
|
|
||
| test('dispatches astro:hydration-error and avoids unhandled rejections on persistent failure', async ({ | ||
| page, | ||
| astro, | ||
| }) => { | ||
| const pageUrl = astro.resolveUrl('/'); | ||
| const html = await (await page.request.get(pageUrl)).text(); | ||
| const componentUrl = getIslandComponentUrl(html); | ||
|
|
||
| const pageErrors = []; | ||
| const consoleErrors = []; | ||
| page.on('pageerror', (error) => pageErrors.push(error.message)); | ||
| page.on('console', (message) => { | ||
| if (message.type() === 'error') consoleErrors.push(message.text()); | ||
| }); | ||
|
|
||
| await page.route(`**${componentUrl.split('?')[0]}*`, async (route) => { | ||
| await route.abort('failed'); | ||
| }); | ||
|
|
||
| await page.goto(pageUrl); | ||
| await page.waitForFunction(() => window.__hydrationErrorEvents?.length === 1); | ||
|
|
||
| const hydrationErrors = await page.evaluate(() => window.__hydrationErrorEvents); | ||
| expect(hydrationErrors).toHaveLength(1); | ||
| expect(hydrationErrors[0].componentUrl).toContain(componentUrl.split('?')[0]); | ||
| expect(pageErrors).toEqual([]); | ||
| expect( | ||
| consoleErrors.some((message) => message.includes('[astro-island] Error hydrating')), | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| function getIslandComponentUrl(html) { | ||
| const match = html.match(/component-url="([^"]+)"/); | ||
| if (!match) { | ||
| throw new Error('Failed to find astro-island component-url in page HTML'); | ||
| } | ||
| return match[1]; | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/astro/e2e/fixtures/astro-island-hydration-error/astro.config.mjs
This file contains hidden or 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,6 @@ | ||
| import react from '@astrojs/react'; | ||
| import { defineConfig } from 'astro/config'; | ||
|
|
||
| export default defineConfig({ | ||
| integrations: [react()], | ||
| }); |
11 changes: 11 additions & 0 deletions
11
packages/astro/e2e/fixtures/astro-island-hydration-error/package.json
This file contains hidden or 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 @@ | ||
| { | ||
| "name": "@e2e/astro-island-hydration-error", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@astrojs/react": "workspace:*", | ||
| "astro": "workspace:*", | ||
| "react": "^18.3.1", | ||
| "react-dom": "^18.3.1" | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
packages/astro/e2e/fixtures/astro-island-hydration-error/src/components/Counter.jsx
This file contains hidden or 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,13 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| export default function Counter() { | ||
| const [count, setCount] = useState(0); | ||
| return ( | ||
| <div id="counter"> | ||
| <button className="increment" onClick={() => setCount((value) => value + 1)}> | ||
| + | ||
| </button> | ||
| <pre>{count}</pre> | ||
| </div> | ||
| ); | ||
| } |
21 changes: 21 additions & 0 deletions
21
packages/astro/e2e/fixtures/astro-island-hydration-error/src/pages/index.astro
This file contains hidden or 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,21 @@ | ||
| --- | ||
| import Counter from '../components/Counter.jsx'; | ||
| --- | ||
|
|
||
| <html> | ||
| <head> | ||
| <script> | ||
| window.__hydrationErrorEvents = []; | ||
| window.addEventListener('astro:hydration-error', (event) => { | ||
| window.__hydrationErrorEvents.push({ | ||
| componentUrl: event.detail?.componentUrl, | ||
| hasError: !!event.detail?.error, | ||
| }); | ||
| event.preventDefault(); | ||
| }); | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <Counter client:load /> | ||
| </body> | ||
| </html> |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new feature so should be
minor.Also make the changeset a little more in-depth, show a code example of how you might use this.