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 "Invalid hook call" warning #3769

Merged
merged 4 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/lazy-countries-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/preact': patch
---

Fix "Invalid hook call" warning
62 changes: 55 additions & 7 deletions packages/integrations/preact/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,74 @@ import StaticHtml from './static-html.js';

const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());

let originalConsoleError;
let runningChecks = 0;
delucis marked this conversation as resolved.
Show resolved Hide resolved

function check(Component, props, children) {
if (typeof Component !== 'function') return false;

if (Component.prototype != null && typeof Component.prototype.render === 'function') {
return BaseComponent.isPrototypeOf(Component);
}

installConsoleErrorFilter();

runningChecks++;
hippotastic marked this conversation as resolved.
Show resolved Hide resolved
try {
const { html } = renderToStaticMarkup(Component, props, children);
if (typeof html !== 'string') {
try {
const { html } = renderToStaticMarkup(Component, props, children);
if (typeof html !== 'string') {
return false;
}

// There are edge cases (SolidJS) where Preact *might* render a string,
// but components would be <undefined></undefined>

return !/\<undefined\>/.test(html);
} catch (err) {
return false;
}
} finally {
runningChecks--;
}
}

/**
* Attempts to filter `console.error` by passing all calls through
* the `filteredConsoleError` function.
*/
function installConsoleErrorFilter() {
// If we already tried to hook `console.error`,
// there is no need to do it again
if (originalConsoleError)
return;
// eslint-disable-next-line no-console
originalConsoleError = console.error;

// There are edge cases (SolidJS) where Preact *might* render a string,
// but components would be <undefined></undefined>
try {
// eslint-disable-next-line no-console
console.error = filteredConsoleError;
} catch (error) {
// If we're unable to hook `console.error`, just accept it
}
}

return !/\<undefined\>/.test(html);
} catch (err) {
return false;
/**
* Reduces console noise by filtering known non-problematic errors
* while any calls to the `check` function are running.
*/
function filteredConsoleError(msg, ...rest) {
if (runningChecks > 0 && typeof msg === 'string') {
// In `check`, we attempt to render JSX components through Preact.
// When attempting this on a React component, React may output
// the following error, which we can safely filter out:
const isKnownReactHookError =
msg.includes('Warning: Invalid hook call.') &&
msg.includes('https://reactjs.org/link/invalid-hook-call');
if (isKnownReactHookError)
return;
}
originalConsoleError(msg, ...rest);
}

function renderToStaticMarkup(Component, props, { default: children, ...slotted }) {
Expand Down