Skip to content

Commit

Permalink
Prevent hydration mismatches in Preact (#6215)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Feb 13, 2023
1 parent b46787c commit a7f1805
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-chicken-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/preact': patch
---

Prevent hydration mismatches in Preact
14 changes: 12 additions & 2 deletions packages/integrations/preact/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, render } from 'preact';
import { h, type JSX, render } from 'preact';
import StaticHtml from './static-html.js';
import type { SignalLike } from './types';

Expand Down Expand Up @@ -26,8 +26,18 @@ export default (element: HTMLElement) =>
props[propName] = sharedSignalMap.get(signalId);
}
}

// eslint-disable-next-line @typescript-eslint/no-shadow
function Wrapper({ children }: { children: JSX.Element }) {
let attrs = Object.fromEntries(Array.from(element.attributes).map(attr => [attr.name, attr.value]));
return h(element.localName, attrs, children);
}

render(
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children),
h(Wrapper, null,
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children)
),
element.parentNode!,
element
);
};

0 comments on commit a7f1805

Please sign in to comment.