Skip to content

Commit

Permalink
Simplify nested hydration flow (#7370)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jun 27, 2023
1 parent da56a00 commit 8301679
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-tables-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Simplify nested hydration flow
4 changes: 3 additions & 1 deletion .github/scripts/bundle-size.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default async function checkBundleSize({ github, context }) {
...context.repo,
pull_number: PR_NUM,
});
const clientRuntimeFiles = files.filter(({ filename }) => filename.startsWith(CLIENT_RUNTIME_PATH));
const clientRuntimeFiles = files.filter((file) => {
return file.filename.startsWith(CLIENT_RUNTIME_PATH) && file.status !== 'removed'
});
if (clientRuntimeFiles.length === 0) return;

const table = [
Expand Down
20 changes: 0 additions & 20 deletions packages/astro/src/runtime/client/events.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/astro/src/runtime/client/hydration-directives.d.ts

This file was deleted.

29 changes: 16 additions & 13 deletions packages/astro/src/runtime/server/astro-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ declare const Astro: {
}
}
async childrenConnectedCallback() {
window.addEventListener('astro:hydrate', this.hydrate);
let beforeHydrationUrl = this.getAttribute('before-hydration-url');
if (beforeHydrationUrl) {
await import(beforeHydrationUrl);
Expand Down Expand Up @@ -96,17 +95,22 @@ declare const Astro: {
this
);
}
hydrate = () => {
if (
!this.hydrator ||
// 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]')
) {
hydrate = async () => {
// The client directive needs to load the hydrator code before it can hydrate
if (!this.hydrator) return;

// 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.
if (!this.isConnected) return;

// Wait for parent island to hydrate first so we hydrate top-down. The `ssr` attribute
// represents that it has not completed hydration yet.
const parentSsrIsland = this.parentElement?.closest('astro-island[ssr]');
if (parentSsrIsland) {
parentSsrIsland.addEventListener('astro:hydrate', this.hydrate, { once: true });
return;
}

const slotted = this.querySelectorAll('astro-slot');
const slots: Record<string, string> = {};
// Always check to see if there are templates.
Expand All @@ -126,12 +130,11 @@ declare const Astro: {
const props = this.hasAttribute('props')
? JSON.parse(this.getAttribute('props')!, reviver)
: {};
this.hydrator(this)(this.Component, props, slots, {
await this.hydrator(this)(this.Component, props, slots, {
client: this.getAttribute('client'),
});
this.removeAttribute('ssr');
window.removeEventListener('astro:hydrate', this.hydrate);
window.dispatchEvent(new CustomEvent('astro:hydrate'));
this.dispatchEvent(new CustomEvent('astro:hydrate'));
};
attributeChangedCallback() {
this.hydrate();
Expand Down

0 comments on commit 8301679

Please sign in to comment.