-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for React 18 in @astrojs/react (#2947)
* First pass at supporting React 18 in @astrojs/react * Try marking React 18’s `react-dom/client` as external * Try a different approach to importing different React versions * Allow resolving JSON modules * Revert "Allow resolving JSON modules" This reverts commit 5279b72. * Try the separate client entrypoint approach from #2946 * Clean up diff * Trying to see something * Just keep swimming… 🐠 * update to support react 18 * add changeset * add docs Co-authored-by: delucis <[email protected]>
- Loading branch information
1 parent
25c1abf
commit 57f48b2
Showing
9 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/react': minor | ||
--- | ||
|
||
Add support for React v18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createElement } from 'react'; | ||
import { hydrate } from 'react-dom'; | ||
import StaticHtml from './static-html.js'; | ||
|
||
export default (element) => (Component, props, children) => | ||
hydrate( | ||
createElement( | ||
Component, | ||
{ ...props, suppressHydrationWarning: true }, | ||
children != null ? createElement(StaticHtml, { value: children, suppressHydrationWarning: true }) : children | ||
), | ||
element | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { createElement } from 'react'; | ||
import { hydrate } from 'react-dom'; | ||
import { hydrateRoot } from 'react-dom/client'; | ||
import StaticHtml from './static-html.js'; | ||
|
||
export default (element) => (Component, props, children) => | ||
hydrate( | ||
hydrateRoot( | ||
element, | ||
createElement( | ||
Component, | ||
{ ...props, suppressHydrationWarning: true }, | ||
children != null ? createElement(StaticHtml, { value: children, suppressHydrationWarning: true }) : children | ||
), | ||
element | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/server.js'; | ||
import StaticHtml from './static-html.js'; | ||
|
||
const reactTypeof = Symbol.for('react.element'); | ||
|
||
function errorIsComingFromPreactComponent(err) { | ||
return err.message && (err.message.startsWith("Cannot read property '__H'") || err.message.includes("(reading '__H')")); | ||
} | ||
|
||
function check(Component, props, children) { | ||
// Note: there are packages that do some unholy things to create "components". | ||
// Checking the $$typeof property catches most of these patterns. | ||
if (typeof Component === 'object') { | ||
const $$typeof = Component['$$typeof']; | ||
return $$typeof && $$typeof.toString().slice('Symbol('.length).startsWith('react'); | ||
} | ||
if (typeof Component !== 'function') return false; | ||
|
||
if (Component.prototype != null && typeof Component.prototype.render === 'function') { | ||
return React.Component.isPrototypeOf(Component) || React.PureComponent.isPrototypeOf(Component); | ||
} | ||
|
||
let error = null; | ||
let isReactComponent = false; | ||
function Tester(...args) { | ||
try { | ||
const vnode = Component(...args); | ||
if (vnode && vnode['$$typeof'] === reactTypeof) { | ||
isReactComponent = true; | ||
} | ||
} catch (err) { | ||
if (!errorIsComingFromPreactComponent(err)) { | ||
error = err; | ||
} | ||
} | ||
|
||
return React.createElement('div'); | ||
} | ||
|
||
renderToStaticMarkup(Tester, props, children, {}); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
return isReactComponent; | ||
} | ||
|
||
function renderToStaticMarkup(Component, props, children, metadata) { | ||
delete props['class']; | ||
const vnode = React.createElement(Component, { | ||
...props, | ||
children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined, | ||
}); | ||
let html; | ||
if (metadata && metadata.hydrate) { | ||
html = ReactDOM.renderToString(vnode); | ||
} else { | ||
html = ReactDOM.renderToStaticMarkup(vnode); | ||
} | ||
return { html }; | ||
} | ||
|
||
export default { | ||
check, | ||
renderToStaticMarkup, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.