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

feat: improve support for third-party React packages #1701

Merged
merged 1 commit into from
Oct 29, 2021
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
5 changes: 5 additions & 0 deletions .changeset/tall-crabs-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/renderer-react': patch
---

Improve support for third-party React packages
8 changes: 7 additions & 1 deletion packages/renderers/renderer-react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function errorIsComingFromPreactComponent(err) {
}

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') {
Expand Down Expand Up @@ -41,10 +47,10 @@ function check(Component, props, children) {
}

function renderToStaticMarkup(Component, props, children, metadata) {
delete props['class'];
const vnode = React.createElement(Component, {
...props,
children: React.createElement(StaticHtml, { value: children }),
innerHTML: children,
});
let html;
if (metadata && metadata.hydrate) {
Expand Down