Skip to content
Source
Sign inSign up
TanStack/router

Router defaultPendingComponent Not effective(version:1.121.24) #4467

Closedyibird(GitHub profile, opens in a new tab) opened this issue 12 comments

Which project does this relate to?

Router

Describe the bug

import {
  createRootRoute,
  createRouter,
  lazyRouteComponent,
  RouterProvider
} from '@tanstack/react-router'

function Loading() {
  return (
    <div
      style={{
        position: 'fixed',
        left: 0,
        top: 0,
        backgroundColor: 'red',
        width: '100%',
        height: '100%'
      }}
    >
      loading
    </div>
  )
}

const RootRoute = createRootRoute({
  component: lazyRouteComponent(() => import('./Comp01')),
  pendingComponent: Loading
})

function RouterApp() {
  const router = createRouter({
    routeTree: RootRoute,
    defaultPreload: 'intent',
    scrollRestoration: true,
    defaultPendingComponent: Loading
  })
  return <RouterProvider router={router} />
}

export default function Demo() {
  return <RouterApp />
}

Your Example Website or App

code route

Steps to Reproduce the Bug or Issue

import {
  createRootRoute,
  createRouter,
  lazyRouteComponent,
  RouterProvider
} from '@tanstack/react-router'

function Loading() {
  return (
    <div
      style={{
        position: 'fixed',
        left: 0,
        top: 0,
        backgroundColor: 'red',
        width: '100%',
        height: '100%'
      }}
    >
      loading
    </div>
  )
}

const RootRoute = createRootRoute({
  component: lazyRouteComponent(() => import('./Comp01')),
  pendingComponent: Loading
})

function RouterApp() {
  const router = createRouter({
    routeTree: RootRoute,
    defaultPreload: 'intent',
    scrollRestoration: true,
    defaultPendingComponent: Loading
  })
  return <RouterProvider router={router} />
}

export default function Demo() {
  return <RouterApp />
}

Expected behavior

I hope there will be a loading effect when refreshing the webpage, but now the loading component is not working. I don't know if what I wrote is correct, but I am referring to the example on the official website

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

  1. Even if a beforeLoad or loader exists on the root route, the PendingComponent is not displayed.

  2. does this need wrapInSuspense:true on the root node?

  3. does this need wrapInSuspense:true on the root node?

    I have used it, but it has no effect

  4. const RootRoute = createRootRoute({
    component: lazyRouteComponent(() => import('./Comp01')),
    pendingComponent: Loading,
    loader: () => new Promise((resolve) => setTimeout(resolve, 1000))
    })

    Setting the loader is effective, but I expect it to display 'pending component' instead of 'white screen' during route loading

  5. do we have an example that is using a lazy root component? I am not sure this is supported

  6. leesb971204(GitHub profile, opens in a new tab)commented

    It doesn’t seem to be an issue with the lazy component. Even when defining the component without using lazy, the pendingComponent defined in createRootRoute doesn’t render. However, the defaultPendingComponent does render.

    Also, for me, wrapInSuspense: true does have an effect. why?

  7. mversic(GitHub profile, opens in a new tab)commented

    It doesn’t seem to be an issue with the lazy component.

    t is my observation that the pending component is not having effect on lazy routes.

    Even when defining the component without using lazy, the pendingComponent defined in createRootRoute doesn’t render.

    yes, it's possible that it's also not working for root route in general. You should try using a pending component on a lazy route that is not root route to observe the bug

  8. @mversic can you please provide a complete minimal reproducer?

  9. leesb971204(GitHub profile, opens in a new tab)commented

    It doesn’t seem to be an issue with the lazy component.

    t is my observation that the pending component is not having effect on lazy routes.

    Even when defining the component without using lazy, the pendingComponent defined in createRootRoute doesn’t render.

    yes, it's possible that it's also not working for root route in general. You should try using a pending component on a lazy route that is not root route to observe the bug

    I’m not entirely sure if I understood everything correctly, but it seems like the bug does not occur on lazy route that are not the root route.

    I created two test cases:

    1. When the root route is lazy (https://stackblitz.com/edit/tanstack-router-yetyygmq?file=src%2Fmain.tsx)
    2. When a non-root route is lazy (https://stackblitz.com/edit/tanstack-router-t3j5bz2q?file=src%2Froutes%2Fabout.tsx)

    Here’s the summary of what I observed:

    ⸻

    When the root route is lazy:

    1. If only loader is defined → defaultPendingComponent is shown
    2. If both loader and wrapInSuspense are set → the route’s own pendingComponent is shown
    3. If only wrapInSuspense is defined → no pendingComponent is shown
    4. If neither is set → no pendingComponent is shown ⸻

    When a non-root route is lazy: Even without defining a loader, the route’s own pendingComponent is displayed - In fact, even if you remove the loader from the about route in the second link, the pendingComponent still displays correctly when navigating to the about page.

    Please let me know if I’m misunderstanding anything!

  10. Running into the same issue on 1.149.3. Adding loader: () => {} to each lazy route fixes it but this is not an ideal solution

  11. Same here with 1.168.2 . Adding loader: () => {} to each lazy route fixes it.

  12. @deivisonrpg do you have a lazy root route? this is not supported (and doesnt make sense IMO as it will be loaded always )

Sign in to join this conversation.