docusaurus-openapi-docs icon indicating copy to clipboard operation
docusaurus-openapi-docs copied to clipboard

fix: add SSR guard to MethodEndpoint for React 19 compatibility

Open stuartbuckell opened this issue 1 year ago • 2 comments

Summary

This PR fixes build and hydration failures when using React 19 with Docusaurus, addressing multiple compatibility issues.

Problems Fixed

1. Redux Context Unavailable During SSR/Hydration

When building with React 19, the static site generation (SSR) and client-side hydration fail with:

TypeError: Cannot read properties of null (reading 'store')
    at useSelector (server.bundle.js:...)

2. Empty oneOf/anyOf Arrays Cause Tab Errors

OpenAPI specs with empty oneOf or anyOf arrays (e.g., "oneOf": []) cause:

Docusaurus error: the <Tabs> component requires at least one <TabItem> children component

Solutions

Redux Hook Wrappers (hooks.ts)

Wrap useTypedDispatch and useTypedSelector with try-catch blocks to gracefully handle cases where the Redux context is unavailable:

  • useTypedDispatch: Returns a no-op function when context is unavailable
  • useTypedSelector: Returns undefined when context is unavailable, then updates via useEffect once the context becomes available

Empty Schema Array Guard (Schema/index.tsx)

Added early return in AnyOneOf component when oneOf/anyOf array is empty or undefined.

SchemaTabs Empty Children Guard (SchemaTabs/index.tsx)

Added guards to handle empty or null children before passing to sanitizeTabsChildren:

  • Filters out null/undefined children before processing
  • Wraps sanitizeTabsChildren in try-catch
  • Returns null if no valid children exist

Related Issues

  • Partially addresses #1130 (React 19 compatibility)

Testing

Tested with:

  • Docusaurus 3.9.2
  • React 19.2.0
  • docusaurus-theme-openapi-docs 4.5.1

Both npm run build and client-side navigation now work without crashes, including pages with empty oneOf/anyOf schemas.

stuartbuckell avatar Dec 02 '25 21:12 stuartbuckell

Visit the preview URL for this PR (updated for commit 1bd037c):

https://docusaurus-openapi-36b86--pr1257-t1drdogq.web.app

(expires Thu, 15 Jan 2026 22:18:23 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: bf293780ee827f578864d92193b8c2866acd459f

github-actions[bot] avatar Dec 16 '25 22:12 github-actions[bot]

Thanks for the contribution @stuartbuckell, please find my comments below:

Concern with hooks.ts:

The try-catch wrapper adds complexity and the comments mention "react-redux v7.x", but this library uses v9.2.0. If components properly guard themselves (like MethodEndpoint now does), the hook wrapper shouldn't be necessary.

Can you clarify:

  • Are you testing with react-redux v7.x or v9.x?
  • Are there other components hitting this issue that aren't guarded?

If the MethodEndpoint pattern works, I'd prefer applying that pattern to other affected components rather than wrapping all hooks globally, if that makes sense.

sserrata avatar Dec 17 '25 16:12 sserrata

Following up with a more detailed review...


✅ Schema/index.tsx - Approved

The guard for empty oneOf/anyOf arrays is a good defensive fix. Empty arrays are valid in OpenAPI specs but would cause the Tabs component to throw. Clean and minimal fix.


✅ SchemaTabs/index.tsx - Approved

The changes properly guard against empty/null children before calling sanitizeTabsChildren. This prevents the "Tabs requires at least one TabItem" error. The approach is defensive and appropriate.


⚠️ MethodEndpoint/index.tsx - Needs Minor Revision

The SSR guard pattern using ExecutionEnvironment.canUseDOM is a reasonable approach. However, the comment references the wrong react-redux version ("v7.x") when this library uses v9.2.0. Please update or simplify the comment to remove the version reference.


❌ hooks.ts - Needs Revision

This change has several issues:

  1. Incorrect version reference: Comments mention "react-redux v7.x" but the library uses v9.2.0

  2. Violates Rules of Hooks: The useTypedSelector implementation uses conditional hook execution via try-catch, which goes against React's rules of hooks

  3. Behavioral changes: The replacement uses useState and useEffect, which returns undefined initially then updates via effect, could cause UI flicker, and changes the synchronous nature of useSelector to async

  4. Global scope impact: Modifying shared hooks affects all consumers, not just the problematic SSR scenarios

Recommendation: Remove the hooks.ts changes and apply component-level SSR guards (like the MethodEndpoint pattern) to specific components that need them. This is a more targeted approach that doesn't risk unintended side effects across the codebase.


Summary

File Status Action Required
Schema/index.tsx ✅ Approved None
SchemaTabs/index.tsx ✅ Approved None
MethodEndpoint/index.tsx ⚠️ Approved with changes Update comment to remove v7.x reference
hooks.ts ❌ Changes requested Remove these changes; use component-level guards

Next Steps

  1. Remove the hooks.ts changes entirely
  2. Update the comment in MethodEndpoint/index.tsx
  3. If there are other components hitting this Redux context issue during SSR, please identify them so we can apply the same ExecutionEnvironment.canUseDOM guard pattern

Let me know if you have any questions!

sserrata avatar Jan 26 '26 19:01 sserrata

Thanks for the contribution @stuartbuckell. Since this PR has been open for a while without updates, I've taken ownership of the approved changes and opened #1363 which applies:

  • ✅ Schema/index.tsx — empty oneOf/anyOf guard
  • ✅ SchemaTabs/index.tsx — null/empty children guard
  • ✅ MethodEndpoint/index.tsx — SSR guard (with corrected comment)

The hooks.ts changes were excluded as they violate Rules of Hooks. Closing this in favour of #1363.

sserrata avatar Mar 30 '26 16:03 sserrata