diff options
author | 2022-08-10 15:10:31 -0400 | |
---|---|---|
committer | 2022-08-10 15:10:31 -0400 | |
commit | 0af5aa7a3b2a858c6b6cdbeda46bc3d90be1250f (patch) | |
tree | b4f9dc145005c31c0d0f4cce66a30130908f650c /packages/integrations/solid/src/client.ts | |
parent | 58941e93c396bf35becc7431e1743afbaad6dd69 (diff) | |
download | astro-0af5aa7a3b2a858c6b6cdbeda46bc3d90be1250f.tar.gz astro-0af5aa7a3b2a858c6b6cdbeda46bc3d90be1250f.tar.zst astro-0af5aa7a3b2a858c6b6cdbeda46bc3d90be1250f.zip |
Fix solid recursion bug (#4215)
* Fix solid recursion bug
* Fix types
* Remove debug code
* Remove logging from e2e test
Diffstat (limited to 'packages/integrations/solid/src/client.ts')
-rw-r--r-- | packages/integrations/solid/src/client.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/packages/integrations/solid/src/client.ts b/packages/integrations/solid/src/client.ts new file mode 100644 index 000000000..b58bdd0b8 --- /dev/null +++ b/packages/integrations/solid/src/client.ts @@ -0,0 +1,45 @@ +import { sharedConfig } from 'solid-js'; +import { hydrate, render, createComponent } from 'solid-js/web'; + +export default (element: HTMLElement) => + (Component: any, props: any, slotted: any, { client }: { client: string }) => { + // Prepare global object expected by Solid's hydration logic + if (!(window as any)._$HY) { + (window as any)._$HY = { events: [], completed: new WeakSet(), r: {} }; + } + if (!element.hasAttribute('ssr')) return; + + const fn = client === 'only' ? render : hydrate; + + 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; + } + } + } + + const { default: children, ...slots } = _slots; + const renderId = element.dataset.solidRenderId; + + fn( + () => + createComponent(Component, { + ...props, + ...slots, + children, + }), + element, + { + renderId + } + ); + }; |