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

Prevent throwing in react and solid component checks #11624

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/few-starfishes-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/solid-js': patch
---

Prevents throwing errors when checking if a component is a Solid component in runtime
5 changes: 5 additions & 0 deletions .changeset/spotty-lies-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Prevents throwing errors when checking if a component is a React component in runtime
20 changes: 9 additions & 11 deletions packages/integrations/preact/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ async function check(
useConsoleFilter();

try {
try {
Comment on lines 29 to -30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only refactored this part as the nested try isn't necessary. Before it's try { try..catch } finally {}. It can be flatten as try..catch..finally and work the same.

const { html } = await renderToStaticMarkup.call(this, Component, props, children, undefined);
if (typeof html !== 'string') {
return false;
}

// There are edge cases (SolidJS) where Preact *might* render a string,
// but components would be <undefined></undefined>
// It also might render an empty sting.
return html == '' ? false : !/<undefined>/.test(html);
} catch (err) {
const { html } = await renderToStaticMarkup.call(this, Component, props, children, undefined);
if (typeof html !== 'string') {
return false;
}

// There are edge cases (SolidJS) where Preact *might* render a string,
// but components would be <undefined></undefined>
// It also might render an empty sting.
return html == '' ? false : !/<undefined>/.test(html);
} catch (err) {
return false;
} finally {
finishUsingConsoleFilter();
}
Expand Down
18 changes: 1 addition & 17 deletions packages/integrations/react/server-v17.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import StaticHtml from './static-html.js';
const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
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.
Expand All @@ -26,28 +18,20 @@ function check(Component, props, children) {
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;
}
}
} catch {}

return React.createElement('div');
}

renderToStaticMarkup(Tester, props, children, {});

if (error) {
throw error;
}
return isReactComponent;
}

Expand Down
18 changes: 1 addition & 17 deletions packages/integrations/react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ import StaticHtml from './static-html.js';
const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
const reactTypeof = Symbol.for('react.element');

function errorIsComingFromPreactComponent(err) {
return (
err.message &&
(err.message.startsWith("Cannot read property '__H'") ||
err.message.includes("(reading '__H')"))
);
}

async 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.
Expand All @@ -32,28 +24,20 @@ async function check(Component, props, children) {
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;
}
}
} catch {}

return React.createElement('div');
}

await renderToStaticMarkup(Tester, props, children, {});

if (error) {
throw error;
}
return isReactComponent;
}

Expand Down
16 changes: 10 additions & 6 deletions packages/integrations/solid/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ async function check(
// In general, components from other frameworks (eg, MDX, React, etc.) tend to render as "undefined",
// so we take advantage of this trick to decide if this is a Solid component or not.

const { html } = await renderToStaticMarkup.call(this, Component, props, children, {
// The purpose of check() is just to validate that this is a Solid component and not
// React, etc. We should render in sync mode which should skip Suspense boundaries
// or loading resources like external API calls.
renderStrategy: 'sync' as RenderStrategy,
});
let html: string | undefined;
try {
const result = await renderToStaticMarkup.call(this, Component, props, children, {
// The purpose of check() is just to validate that this is a Solid component and not
// React, etc. We should render in sync mode which should skip Suspense boundaries
// or loading resources like external API calls.
renderStrategy: 'sync' as RenderStrategy,
});
html = result.html;
} catch {}

return typeof html === 'string';
}
Expand Down
Loading