Skip to content

Invalid hook call during dev SSR with @astrojs/cloudflare and a React island #17834

Description

@valkum

Astro Info

Astro                    v7.2.1
Node                     v22.21.1
System                   macOS (arm64)
Package Manager          bun
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

This is a follow-up to #16529, which was closed with "We believe this is likely fixed in Astro 7." The error is still present on Astro 7.2.1.

Repro:

  1. bun install (or npm install)
  2. rm -rf node_modules/.vite
  3. bunx astro dev
  4. Request the page one time: curl -s http://localhost:4321/ | wc -c
  5. Request the page a second time.

The first response is 420 bytes and holds no astro-island element. The server logs:

[ERROR] [vite] Invalid hook call. Hooks can only be called inside of the body of a function component.
[ERROR] [vite] TypeError: Cannot read properties of null (reading 'useState')
    at Object.exports.useState (node_modules/react/cjs/react.development.js:1263:33)
    at ReproCounter (src/components/ReproCounter.tsx:4:29)
    at renderWithHooks (node_modules/.vite/deps_ssr/react-dom_server.js:2797:19)

This is caused by using React and the Cloudflare adapter. The docs do not mention any caveats when using both.

What's the expected result?

The first response renders the island.

Link to Minimal Reproducible Example

https://github.com/valkum/astro-7-react-cloudflare-issue

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage: unable to reproduceTriage bot attempted reproduction but could not reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions