Skip to content

Uncaught clientLoader errors in React 18 (specific to presence of useEffect adding/removing script in the root.tsx layout export) #13106

@gundam-wing

Description

@gundam-wing

I'm using React Router as a...

framework

Reproduction

reproduction: https://stackblitz.com/edit/github-4zctv3wf?file=app%2Froot.tsx

I've run into an odd issue using react-router framework mode with react 18 (I also noticed the issue does not seem to be present in 19). If I have the following component in my root.tsx Layout export,

function TestScript() {
  useEffect(() => {
    const script = document.createElement("script");
    document.head.appendChild(script)
    return () => {
      if (script) {
        document.head.removeChild(script);
      }
    }
  }, []);
  return null;
};

And then a clientLoader throws an error, rather than displaying the root ErrorBoundary, I cycle through error messages in the browser console and the app becomes unresponsive.

demo_rr.mov

System Info

System:
  OS: Linux 5.0 undefined
  CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 0 Bytes / 0 Bytes
  Shell: 1.0 - /bin/jsh
Binaries:
  Node: 18.20.3 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.2.3 - /usr/local/bin/npm
  pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
  @react-router/dev: * => 7.2.0 
  @react-router/node: * => 7.2.0 
  @react-router/serve: * => 7.2.0 
  react-router: * => 7.2.0 
  vite: ^6.0.11 => 6.1.1

Used Package Manager

npm

Expected Behavior

When an Error occurs in the clientLoader, the Error should be caught by the root.tsx ErrorBoundary and display the ErrorBoundary jsx.

Actual Behavior

When an Error occurs in the clientLoader, a loop of errors prints in the console and the application becomes unresponsive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions