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 client hydration in experimentalReactChildren #8898

Merged
merged 8 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/real-dryers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Fixes client hydration in islands when using experimentalReactChildren
39 changes: 38 additions & 1 deletion packages/integrations/react/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@ function isAlreadyHydrated(element) {
}
}

function createReactElementFromDOMElement(element) {
let attrs = {};
for(const attr of element.attributes) {
attrs[attr.name] = attr.value;
}

return createElement(element.localName, attrs,
Array.from(element.childNodes).map(c => {
if(c.nodeType === Node.TEXT_NODE) {
return c.data;
} else if(c.nodeType === Node.ELEMENT_NODE) {
return createReactElementFromDOMElement(c)
} else {
return undefined;
}
}).filter(a => !!a)
);
}

function getChildren(experimentalReactChildren, childString) {
if(experimentalReactChildren && childString) {
let children = [];
let template = document.createElement('template');
template.innerHTML = childString;
for(let child of template.content.children) {
children.push(createReactElementFromDOMElement(child))
}
return children;
} else if(childString) {
return createElement(StaticHtml, { value: childString });
} else {
return undefined;
}

}

export default (element) =>
(Component, props, { default: children, ...slotted }, { client }) => {
if (!element.hasAttribute('ssr')) return;
Expand All @@ -19,10 +55,11 @@ export default (element) =>
for (const [key, value] of Object.entries(slotted)) {
props[key] = createElement(StaticHtml, { value, name: key });
}

const componentEl = createElement(
Component,
props,
children != null ? createElement(StaticHtml, { value: children }) : children
getChildren(element.hasAttribute('data-react-children'), children)
matthewp marked this conversation as resolved.
Show resolved Hide resolved
);
const rootKey = isAlreadyHydrated(element);
// HACK: delete internal react marker for nested components to suppress aggressive warnings
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl
};
const newChildren = children ?? props.children;
if (children && opts.experimentalReactChildren) {
attrs['data-react-children'] = true;
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
const convert = await import('./vnode-children.js').then((mod) => mod.default);
newProps.children = convert(children);
} else if (newChildren != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

export default function ({ children }) {
export default function ({ id, children }) {
return (
<div>
<div id={id}>
<div className="with-children">{children}</div>
<div className="with-children-count">{children.length}</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import WithChildren from '../components/WithChildren';
<!-- Head Stuff -->
</head>
<body>
<WithChildren>
<WithChildren id="one">
<div>child 1</div><div>child 2</div>
</WithChildren>

<WithChildren id="two" client:load>
<div>child 1</div><div>child 2</div>
</WithChildren>
</body>
Expand Down
8 changes: 7 additions & 1 deletion packages/integrations/react/test/react-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ describe('React Components', () => {
it('Children are parsed as React components, can be manipulated', async () => {
const html = await fixture.readFile('/children/index.html');
const $ = cheerioLoad(html);
expect($('.with-children-count').text()).to.equal('2');
expect($('#one .with-children-count').text()).to.equal('2');
});

it('Client children passes option to the client', async () => {
const html = await fixture.readFile('/children/index.html');
const $ = cheerioLoad(html);
expect($('[data-react-children]')).to.have.lengthOf(1);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/react/vnode-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function convert(children) {
} else if (node.type === ELEMENT_NODE) {
const { class: className, ...props } = node.attributes;
// NOTE: do not manually pass `children`, React handles this internally
newNode = createElement(node.name, { ...props, className, key: `${id}-${key++}` });
newNode = createElement(node.name, { ...props, className, key: `${id}-${key++}` }, []);
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
nodeMap.set(node, newNode);
if (parent) {
const newParent = nodeMap.get(parent);
Expand Down
Loading