NavigationMenu crashes with Preact in Astro compat mode
Bug report
Current behavior
Base UI does not seem to work properly with Preact in an Astro app using @astrojs/preact.
In my Astro project, I added Base UI and used @base-ui/react/navigation-menu inside a component rendered with client:load. Even after configuring Preact compatibility, the app crashes with:
17:57:09 [ERROR] Cannot read properties of undefined (reading '__H')
Location:
<project-root>/node_modules/.pnpm/[email protected]/node_modules/preact/hooks/src/index.js:151:19
Stack trace:
at getHookState (<project-root>/node_modules/.pnpm/[email protected]/node_modules/preact/hooks/src/index.js:151:19)
at exports.useRef (<project-root>/node_modules/.pnpm/[email protected]/node_modules/preact/hooks/src/index.js:311:9)
at useStoreLegacy (<project-root>/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected][email protected]__@[email protected]/node_modules/@base-ui/utils/esm/store/useStore.js:106:10)
at FloatingRootStore.useState (<project-root>/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected][email protected]__@[email protected]/node_modules/@base-ui/utils/esm/store/ReactStore.js:142:12)
at NavigationMenuList (<project-root>/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]__@preact+c_39c42a3009535fefbc40e16c3cc37286/node_modules/@base-ui/react/esm/navigation-menu/list/NavigationMenuList.js:43:3)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:404:23)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:715:10)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:501:16)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:501:16)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:501:16)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:501:16)
at _renderToString (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:501:16)
at _finallyRethrows (<project-root>/node_modules/.pnpm/[email protected][email protected]/node_modules/preact-render-to-string/src/index.js:578:16)
at Object.renderToStaticMarkup (<project-root>/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]_9da29607664a30e36b9ef4c691a1a242/node_modules/@astrojs/preact/dist/server.js:58:36)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
I already tried the usual compatibility setup:
- added pnpm overrides for react and react-dom to resolve to @preact/compat
- enabled compat mode in Astro with preact({ compat: true }) Relevant versions from the project:
- @base-ui/react: 1.4.1
- @astrojs/preact: 5.0.3
- astro: 6.1.9
- preact: 10.29.1
This looks similar to #1995
Expected behavior
Base UI components should work with Preact when using @astrojs/preact and the standard compat setup (@preact/compat overrides + compat: true).
Base UI version
1.4.1
Which browser are you using?
Chrome
Which OS are you using?
Linux
Additional context
When disabling SSR for that island via client:only="preact" everything works
Codex findings:
- Bug confirmed. I reproduced it in a clean Astro app with
astro 6.1.9,@astrojs/preact 5.1.2,preact 10.29.1,@preact/compat 18.3.2, and@base-ui/react 1.4.1. RenderingNavigationMenuwithclient:loadfails onpnpm buildwith the same__Hstack.- This is not user error. The failing path starts in
packages/utils/src/store/useStore.ts, which importsuse-sync-external-storeshims.- The evidence points to an ESM/CJS interop bug with Preact compat, not
NavigationMenulogic itself:
- CJS SSR works.
- ESM SSR fails.
- A minimal ESM Preact SSR example that imports
use-sync-external-store/shimor/with-selectoralso fails.@preact/compatexposes separateimportandrequireentrypoints, so the shim is very likely pulling a different runtime than Astro’s ESM renderer.- Scope is broader than
NavigationMenu. The same hazard exists inpackages/react/src/utils/useIsHydrating.tsandpackages/react/src/unstable-use-media-query/index.ts.Workaround
client:only="preact"is a valid temporary workaround and matches the issue report.- Forcing CJS also avoids the crash, but that is brittle and not a good user-facing workaround.
Proposed fix
- Add an internal ESM-safe
useSyncExternalStorecompat helper that prefersReact.useSyncExternalStoreunder Preact compat, reusing the existing Preact detection idea frompackages/utils/src/useStableCallback.ts- Replace the external selector shim in packages/utils/src/store/useStore.ts with a local
useSyncExternalStoreWithSelectorwrapper built from React hooks.- Keep the current React 18 behavior unless Preact compat is detected, so we don’t regress the React 18 concerns noted in PR
#3360.- Audit and switch the direct shim consumers in
useIsHydratinganduseMediaQueryto the same helper.I validated this fix shape by patching a throwaway install under
.context; after replacing the selector shim with a local wrapper, the Astro repro build passed.
This also makes SSG in Astro with Preact not viable, as server-side rendering is strictly needed in this context