Skip to content

Commit

Permalink
Fix client:visible directive in safari (#3839)
Browse files Browse the repository at this point in the history
* fix: client visible on safari

* chore: changeset

* refactor: wait for children with mutation observer

* fix: remove unecessary settimeout

* refactor: remove unecessary awaits
  • Loading branch information
bholmesdev committed Jul 6, 2022
1 parent b1c4600 commit cd3f634
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-meals-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `client:visible` directive in safari
17 changes: 15 additions & 2 deletions packages/astro/src/runtime/server/astro-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ declare const Astro: {
public Component: any;
public hydrator: any;
static observedAttributes = ['props'];
async connectedCallback() {
connectedCallback() {
if(this.getAttribute('client') === 'only' || this.firstChild) {
this.childrenConnectedCallback();
} else {
// connectedCallback may run *before* children are rendered (ex. HTML streaming)
// If SSR children are expected, but not yet rendered,
// Wait with a mutation observer
new MutationObserver((_, mo) => {
mo.disconnect();
this.childrenConnectedCallback();
}).observe(this, { childList: true });
}
}
async childrenConnectedCallback() {
window.addEventListener('astro:hydrate', this.hydrate);
await import(this.getAttribute('before-hydration-url')!);
const opts = JSON.parse(this.getAttribute('opts')!) as Record<string, any>;
Expand All @@ -57,7 +70,7 @@ declare const Astro: {
return this.hydrate;
},
opts,
this
this,
);
}
hydrate = () => {
Expand Down

0 comments on commit cd3f634

Please sign in to comment.