Skip to content
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/calm-bats-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/preact': patch
---

Fixed an issue where the Preact integration would incorrectly intercept React 19 components, triggering "Invalid hook call" error logs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import preact from '@astrojs/preact';
import react from '@astrojs/react';
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
integrations: [
// This issue only reproduces when the Preact integration is placed before the React integration.
preact({ include: ['**/preact/*'] }),
react({ include: ['**/react/*'] }),
],
});
15 changes: 15 additions & 0 deletions packages/astro/e2e/fixtures/react19-preact-hook-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@e2e/react19-preact-hook-error",
"version": "0.0.0",
"private": true,
"devDependencies": {
"@astrojs/preact": "workspace:*",
"@astrojs/react": "workspace:*",
"astro": "workspace:*"
},
"dependencies": {
"preact": "^10.28.3",
"react": "^19.2.4",
"react-dom": "^19.2.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ComponentChildren } from 'preact';
import { useState } from 'preact/hooks';

/** A counter written with Preact */
export function PreactCounter({ children }: { children?: ComponentChildren }) {
const [count, setCount] = useState(0);
const add = () => setCount((i) => i + 1);
const subtract = () => setCount((i) => i - 1);

return (
<>
<div class="counter">
<button onClick={subtract}>-</button>
<pre>{count}</pre>
<button onClick={add}>+</button>
</div>
<div class="counter-message">{children}</div>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useState } from 'react';

/** a counter written in React */
export function Counter({ children, id }) {
const [count, setCount] = useState(0);
const add = () => setCount((i) => i + 1);
const subtract = () => setCount((i) => i - 1);

return (
<>
<div id={id} className="counter">
<button className="decrement" onClick={subtract}>-</button>
<pre>{count}</pre>
<button className="increment" onClick={add}>+</button>
</div>
<div className="counter-message">{children}</div>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
// Style Imports
import '../styles/global.css';

import { PreactCounter } from '../components/preact/PreactCounter';
import * as react from '../components/react/ReactCounter';
---

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" href="/favicon.ico" />
</head>
<body>
<main>
<react.Counter client:visible>
<h1>Hello from React!</h1>
</react.Counter>

<PreactCounter client:visible>
<h1>Hello from Preact!</h1>
</PreactCounter>
</main>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
html,
body {
font-family: system-ui;
margin: 0;
}

body {
padding: 2rem;
}

.counter {
display: grid;
font-size: 2em;
grid-template-columns: repeat(3, minmax(0, 1fr));
margin-top: 2em;
place-items: center;
}

.counter-message {
text-align: center;
}
42 changes: 42 additions & 0 deletions packages/astro/e2e/react19-preact-hook-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from '@playwright/test';
import { testFactory } from './test-utils.js';

const test = testFactory(import.meta.url, { root: './fixtures/react19-preact-hook-error/' });

function hookError() {
const error = console.error;
const errors = [];
console.error = function (...args) {
errors.push(args);
};
return () => {
console.error = error;
return errors;
};
}

let devServer;
let unhook;

test.beforeAll(async ({ astro }) => {
devServer = await astro.startDevServer();
unhook = hookError();
});

test.afterAll(async () => {
await devServer.stop();
});

// See: https://github.com/withastro/astro/issues/15341
test.describe('React v19 and preact hook issue', () => {
test('should not have "Invalid hook call" errors', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));

const errors = unhook();
const hasInvalidHookCallErrorLog = errors
.flat()
.some((log) => log.includes('Invalid hook call'));

expect(hasInvalidHookCallErrorLog).toBe(false);
});
});
7 changes: 5 additions & 2 deletions packages/integrations/preact/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ function filteredConsoleError(msg: string, ...rest: any[]) {
// 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');
msg.includes('Invalid hook call.') &&
// for React v18 and earlier
(msg.includes('https://reactjs.org/link/invalid-hook-call') ||
// for React v19 and later
msg.includes('https://react.dev/link/invalid-hook-call'));
if (isKnownReactHookError) return;
}
originalConsoleError(msg, ...rest);
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.