Skip to content

Commit

Permalink
Fix nested astro-island hydration race condition (#7197)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jun 6, 2023
1 parent 64b2b65 commit d72cfa7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-sheep-hunt.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix nested astro-island hydration race condition
11 changes: 8 additions & 3 deletions packages/astro/src/runtime/server/astro-island.ts
Expand Up @@ -53,7 +53,8 @@ declare const Astro: {
// Wait with a mutation observer
new MutationObserver((_, mo) => {
mo.disconnect();
this.childrenConnectedCallback();
// Wait until the next macrotask to ensure children are really rendered
setTimeout(() => this.childrenConnectedCallback(), 0);
}).observe(this, { childList: true });
}
}
Expand Down Expand Up @@ -98,7 +99,11 @@ declare const Astro: {
hydrate = () => {
if (
!this.hydrator ||
(this.parentElement && this.parentElement.closest('astro-island[ssr]'))
// Make sure the island is mounted on the DOM before hydrating. It could be unmounted
// when the parent island hydrates and re-creates this island.
!this.isConnected ||
// Wait for parent island to hydrate first so we hydrate top-down.
this.parentElement?.closest('astro-island[ssr]')
) {
return;
}
Expand Down Expand Up @@ -129,7 +134,7 @@ declare const Astro: {
window.dispatchEvent(new CustomEvent('astro:hydrate'));
};
attributeChangedCallback() {
if (this.hydrator) this.hydrate();
this.hydrate();
}
}
);
Expand Down
1 change: 1 addition & 0 deletions scripts/cmd/prebuild.js
Expand Up @@ -69,6 +69,7 @@ export default async function prebuild(...args) {
sourcefile: filepath,
},
format: 'iife',
target: ['es2018'],
minify,
bundle: true,
write: false,
Expand Down

0 comments on commit d72cfa7

Please sign in to comment.