router: Suspense error: A component suspended while responding to synchronous input

Describe the bug

react-router causes the following React error:

A component suspended while responding to synchronous input.
This will cause the UI to be replaced with a loading indicator. To fix, updates that suspend should be wrapped with startTransition.
Full stack trace as shown in console throwException@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:13798:43 handleError@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:18529:29 renderRootSync@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:18614:26 recoverFromConcurrentError@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:18233:42 performSyncWorkOnRoot@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:18375:28 flushSyncCallbacks@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:8776:30 node_modules/react-dom/cjs/react-dom.development.js/ensureRootIsScheduled/<@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/react-dom_client.js?v=ed25c456:18124:21

CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 div PostsRouteComponent@https://xr7dd3-3001.csb.app/src/main.tsx?t=1702934734809:136:38 MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 RootComponent MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 Matches@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:842:18 RouterProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:1048:24 QueryClientProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-XI5YTT3J.js?v=ed25c456:2592:27 Warning: Error in route match: /posts/post CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 div PostsRouteComponent@https://xr7dd3-3001.csb.app/src/main.tsx?t=1702934734809:136:38 MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 RootComponent MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 Matches@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:842:18 RouterProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:1048:24 QueryClientProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-XI5YTT3J.js?v=ed25c456:2592:27 Warning: Error in route match: /posts/post CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 div PostsRouteComponent@https://xr7dd3-3001.csb.app/src/main.tsx?t=1702934734809:136:38 MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 Outlet2@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:938:25 RootComponent MatchInner@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:903:20 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 SafeFragment@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:865:52 Match@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:867:15 CatchBoundaryImpl@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:649:5 CatchBoundary@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:631:27 Matches@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:842:18 RouterProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-GVDQM46P.js?v=ed25c456:1048:24 QueryClientProvider@https://xr7dd3-3001.csb.app/node_modules/.vite/deps/chunk-XI5YTT3J.js?v=ed25c456:2592:27

Your Example Website or App

https://codesandbox.io/p/devbox/cool-poincare-xr7dd3?file=%2Fsrc%2Fmain.tsx%3A20%2C32

Steps to Reproduce the Bug or Issue

  1. go to https://codesandbox.io/p/devbox/cool-poincare-xr7dd3?file=%2Fsrc%2Fmain.tsx%3A20%2C32
  2. click on posts
  3. click on the first post
  4. click on the second post
  5. error is shown

Expected behavior

no error messages

Screenshots or Videos

https://github.com/TanStack/router/assets/6340397/d994f0c1-fabf-44c3-b261-a8eb2b5fb012

Platform

  • OS: macOS
  • Browser: Firefox
  • Version: 120

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (5 by maintainers)

Most upvoted comments

it would be good to update the documentation as this is not mentioned

Just ran into this issue.

I solved it by wrapping the RouterProvider with Suspense.

import { RouterProvider, createRouter } from "@tanstack/react-router";
import { StrictMode, Suspense } from "react";
import ReactDOM from "react-dom/client";

// Import the generated route tree
import { routeTree } from "./routeTree.gen";

// Create a new router instance
const router = createRouter({ routeTree });

// Register the router instance for type safety
declare module "@tanstack/react-router" {
  interface Register {
    router: typeof router;
  }
}

// Render the app
const rootElement = document.getElementById("root")!;
if (!rootElement.innerHTML) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(
    <StrictMode>
      <Suspense fallback={<div>Loading...</div>}>
        <RouterProvider router={router} />
      </Suspense>
    </StrictMode>
  );
}


I solved it by wrapping the devtools in a Suspense component:

const TanStackRouterDevtools =
  process.env.NODE_ENV === 'production'
    ? () => null
    : lazy(() =>
        import('@tanstack/router-devtools').then((res) => ({
          default: res.TanStackRouterDevtools,
        })),
      );

const ReactQueryDevtools =
  process.env.NODE_ENV === 'production'
    ? () => null
    : lazy(() =>
        import('@tanstack/react-query-devtools').then((res) => ({
          default: res.ReactQueryDevtools,
        })),
      );

const App: FC = () => {
  return (
    <>
      <Suspense fallback={null}>
        <TanStackRouterDevtools />
        <ReactQueryDevtools />
      </Suspense>
    </>
  );
};

@StringKe, I solved it like this:

<Suspense fallback={<LoadingPage />}>
    <RouterProvider router={router} />
</Suspense>

I just ran into this issue RouterProvider needs to be wrapped with Suspense. But why is this error occurring now? I tested this yesterday and it didn’t have this issue. Weird.

The solution here is to add a suspense fallback somewhere for useInfinityQuery to fall back on. TSR doesn’t add any suspense wrappers by default unless you use a loader or pendingComponent explicitly.

Add this to the /posts/$postId route options and you’ll be good to go:

{
  pendingComponent: () => <div>Loading...</div>,
}