Skip to content
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

fix hydration in solid renderer #8365

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-cycles-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/solid-js': patch
---

Fix hydration in Solid renderer
29 changes: 17 additions & 12 deletions packages/integrations/solid/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sharedConfig } from 'solid-js';
import { createComponent, hydrate, render } from 'solid-js/web';

export default (element: HTMLElement) =>
Expand All @@ -11,19 +10,25 @@ export default (element: HTMLElement) =>

const boostrap = client === 'only' ? render : hydrate;

let slot: HTMLElement | null;
let _slots: Record<string, any> = {};
if (Object.keys(slotted).length > 0) {
// hydrating
if (sharedConfig.context) {
element.querySelectorAll('astro-slot').forEach((slot) => {
_slots[slot.getAttribute('name') || 'default'] = slot.cloneNode(true);
});
} else {
for (const [key, value] of Object.entries(slotted)) {
_slots[key] = document.createElement('astro-slot');
if (key !== 'default') _slots[key].setAttribute('name', key);
_slots[key].innerHTML = value;
}
// hydratable
if (client !== "only") {
const iterator = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT, (node) => {
if (node === element) return NodeFilter.FILTER_SKIP
if (node.nodeName === "ASTRO-SLOT") return NodeFilter.FILTER_ACCEPT;
if (node.nodeName === "ASTRO-ISLAND") return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_SKIP;
});
while(slot = iterator.nextNode() as HTMLElement | null)
_slots[slot.getAttribute("name") || "default"] = slot;
}
for (const [key, value] of Object.entries(slotted)) {
if (_slots[key]) continue;
_slots[key] = document.createElement('astro-slot');
if (key !== 'default') _slots[key].setAttribute('name', key);
_slots[key].innerHTML = value;
}
}

Expand Down
Loading