Skip to content

Commit e57ccc5

Browse files
committed
Gallery: the Astro component hydrates again after a view-transition navigation
Under Astro's ClientRouter the component's script runs once per document, so a gallery on the next page stayed dead. It now hydrates on astro:page-load too (hydrate ignores a gallery it already has) and destroys the old page's galleries on astro:before-swap, so their listeners don't leak.
1 parent bf7cdbb commit e57ccc5

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

‎clients/gallery/astro/src/Gallery.astro‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ const html = render(await load(Astro.props));
1919
<Fragment set:html={html} />
2020

2121
<script>
22-
import { hydrate } from "visualci-gallery";
22+
// Every gallery on the page, once (hydrate does nothing on one it already has). With Astro's view transitions the
23+
// script runs once per document, so it hydrates again after each navigation and lets the old page's galleries go.
24+
import { hydrate, type Gallery } from "visualci-gallery";
2325

24-
for (const el of document.querySelectorAll<HTMLElement>("[data-vg]")) hydrate(el);
26+
const live = new Set<Gallery>();
27+
const all = () => {
28+
for (const el of document.querySelectorAll<HTMLElement>("[data-vg]")) live.add(hydrate(el));
29+
};
30+
all();
31+
document.addEventListener("astro:page-load", all);
32+
document.addEventListener("astro:before-swap", () => {
33+
for (const g of live) g.destroy();
34+
live.clear();
35+
});
2536
</script>

0 commit comments

Comments
 (0)