TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
7.77k stars 565 forks source link

`beforeLoad` adds ~400ms delay on page reload #2183

Open cschmatzler opened 3 weeks ago

cschmatzler commented 3 weeks ago

Describe the bug

When adding beforeLoad to a route (even an empty one), the router gets mounted and then displays a white page for approx. 400ms before the actual page is rendered.

Your Example Website or App

https://stackblitz.com/github/tanstack/router/tree/main/examples/react/quickstart-file-based

Steps to Reproduce the Bug or Issue

  1. Fork the file-based QuickStart example on StackBlitz.
  2. Add an beforeLoad: () => {} to any page - I chose the about page.
  3. Go to the page you changed and reload the page in the browser.

Expected behavior

Simply adding the beforeLoad option should not materially change load times.

Screenshots or Videos

https://github.com/user-attachments/assets/eaa57efa-7c2f-4fad-b64d-a9f10017e9cf

Platform

Additional context

No response

mreinertssgr commented 2 weeks ago

I've been experiencing this too, however my code might be a little different. Rendering the actual app is delayed by almost 1 second.

const InnerApp = () => {
    const auth = useAuthStore();

    if (auth.loading) {
        return <div>Loading...</div>;
    }

    return (
        <ThemeProvider theme={lightTheme}>
            <CssBaseline />
            <RouterProvider router={router} context={{ auth }} />
            <Toaster />
        </ThemeProvider>
    );
};

export default () => {
    return (
        <StrictMode>
            <QueryClientProvider client={queryClient}>
                <InnerApp />
            </QueryClientProvider>
        </StrictMode>
    );
};

I also checked the performance tab in devtools and it looks like nothing is happening (the selected range is when nothing is rendered)

image
DaniumX commented 1 week ago

As a temporary fix, add pendingMinMs: 0.

macr commented 4 days ago

I just noticed it happening on all the lazy routes. React profiler shows: Image looking at the offending function: https://github.com/TanStack/router/blob/0e16429b0d8af8d65040c06af3b895f4e10b310a/packages/react-router/src/Match.tsx#L19 It does wait for pendingMinMs ms before loading the match. Is there a catch to just setting pendingMinMs: 0?

Scooter1337 commented 1 day ago

I just saw this issue and added the temporary fix and my app feels way faster now... This needs a fix.