#2842·redoc

RedocStandalone leaks AppStore scroll listeners under React Strict Mode (store built in useMemo)

Author: BillSchumacherCreated Sep 23, 2026Updated Sep 23, 2026

Describe the bug Under React Strict Mode (for example, Next.js App Router in development), <RedocStandalone> leaks AppStore instances. Each leaked store keeps a window scroll listener attached after the component unmounts, so scrolling any later page keeps rewriting the URL hash to a Redoc anchor such as #tag/…/operation/…. The same double-invoke also disposes the store that's actually rendered, so the mounted reference loses its own scroll-spy in development.

Cause: StoreBuilder constructs the store inside useMemo (src/components/StoreBuilder.ts:62-73 on main @ 7a9ec49):

typescript
const store = React.useMemo(() => {
  if (!resolvedSpec) return null;
  try {
    return new AppStore(resolvedSpec, specUrl, options);
  ...
}, [resolvedSpec, specUrl, options]);

new AppStore(...) has a side effect. It constructs a ScrollService (src/services/AppStore.ts:68), whose constructor immediately binds window.addEventListener('scroll', …) (src/services/ScrollService.ts:13-22). Only the store passed to <Redoc> is ever disposed, in Redoc.componentWillUnmount (src/components/Redoc/Redoc.tsx:31-32). Strict Mode calls the useMemo factory twice, and React may discard memoized values at any time, so any store the factory built but never rendered keeps its listener forever. Strict Mode's simulated unmount/remount of <Redoc> also calls store.dispose() on the live store, and componentDidMount → store.onDidMount() doesn't re-bind it.

Expected behavior Unmounting <RedocStandalone> removes every window listener Redoc added, including under Strict Mode. A mounted reference keeps a working scroll-spy in development.

Reproduction [email protected], React 19 (Next.js 16 App Router, next dev, where Strict Mode is on by default):

typescript
'use client';
import { RedocStandalone } from 'redoc';
export default function Docs() {
  return <RedocStandalone specUrl="/openapi.json" options={{ scrollYOffset: 50 }} />;
}

Load the page, client-side navigate to another route, and scroll. Instrumenting window.addEventListener / removeEventListener for 'scroll':

scroll listeners added removed still attached
while the docs page is mounted 4 1 3
after navigating away 4 2 2

Both leaked listeners were added via ScrollService.bind ← new ScrollService ← new AppStore ← StoreBuilder's useMemo, and the URL hash on the next page is rewritten to a #tag/…/operation/… anchor. In a production build (no Strict Mode double-invoke), the same page adds 1 listener and removes 1 on navigation, and the hash is left alone.

Additional context

  • A likely fix is to create the store in an effect, or construct it without side effects and bind listeners in onDidMount, and dispose it in that effect's cleanup instead of in Redoc.componentWillUnmount.
  • Workaround we're using: render <RedocStandalone> into a separate createRoot outside the Strict Mode tree, and unmount() that root when the host container is actually detached.
  • Related but separate: #2282 (the standalone destroy() doesn't unmount the root init created).