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: React - Use "createRoot" instead of "hydrateRoot" for client:only #3337

Merged
merged 5 commits into from
May 12, 2022
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
6 changes: 6 additions & 0 deletions .changeset/small-maps-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/react': patch
---

Fix: remove hydration failures on React v18 by exposing the "client" directive from Astro core.
8 changes: 4 additions & 4 deletions packages/astro/src/runtime/server/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ export async function generateHydrateScript(
);
}

let hydrationSource = ``;

hydrationSource += renderer.clientEntrypoint
const hydrationSource = renderer.clientEntrypoint
? `const [{ ${
componentExport.value
}: Component }, { default: hydrate }] = await Promise.all([import("${await result.resolve(
componentUrl
)}"), import("${await result.resolve(renderer.clientEntrypoint)}")]);
return (el, children) => hydrate(el)(Component, ${serializeProps(props)}, children);
return (el, children) => hydrate(el)(Component, ${serializeProps(
props
)}, children, ${JSON.stringify({ client: hydrate })});
`
: `await import("${await result.resolve(componentUrl)}");
return () => {};
Expand Down
21 changes: 12 additions & 9 deletions packages/integrations/react/client-v17.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { createElement } from 'react';
import { hydrate } from 'react-dom';
import { render, hydrate } from 'react-dom';
import StaticHtml from './static-html.js';

export default (element) => (Component, props, children) =>
hydrate(
createElement(
export default (element) => (Component, props, children, { client }) =>
{
const componentEl = createElement(
Component,
{ ...props, suppressHydrationWarning: true },
props,
children != null
? createElement(StaticHtml, { value: children, suppressHydrationWarning: true })
? createElement(StaticHtml, { value: children })
: children
),
element
);
);
if (client === 'only') {
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
return render(componentEl, element);
}
return hydrate(componentEl, element);
};
21 changes: 12 additions & 9 deletions packages/integrations/react/client.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { createElement } from 'react';
import { hydrateRoot } from 'react-dom/client';
import { createRoot, hydrateRoot } from 'react-dom/client';
import StaticHtml from './static-html.js';

export default (element) => (Component, props, children) =>
hydrateRoot(
element,
createElement(
export default (element) => (Component, props, children, { client }) =>
{
const componentEl = createElement(
Component,
{ ...props, suppressHydrationWarning: true },
props,
children != null
? createElement(StaticHtml, { value: children, suppressHydrationWarning: true })
? createElement(StaticHtml, { value: children })
: children
)
);
);
if (client === 'only') {
return createRoot(element).render(componentEl);
}
return hydrateRoot(element, componentEl);
};