Skip to content
Source
Sign inSign up
TanStack/router

fix(router): context issues #1907

Mergedtannerlinsleymerged 41 commits intomain fromrefs/pull/1907/headmerged by schiller-manuel(GitHub profile, opens in a new tab)

tannerlinsleycommented

It looks big, yes. But it boils down to:

  • Better encapsulation for the different states of the match loading process. That’s really just moving some code into functions like runBeforeLoad, etc
  • A change to use getMatch() and updateMatch exclusively, instead of reading and writing to a mix of local and state variables. This has already gotten rid of a few areas where we were definitely reading from stale data (but likely not getting any bugs reported)
  • Tracking and deduping promises for beforeLoad. This is a very important piece to the fix, since prior to this, we were skipping the before load stage if it was in progress and immediately running the loader. This tracking is what enables each load to dedupe and share in-flight promises for both beforeLoad, component loading, lazy loading, and ultimately loader calls.

fixes #1892 fixes #1886 fixes #1752 fixes #1752 fixes #1827 fixes #1534 fixes #1777 fixes #1814 fixes #1656 fixes #1796 fixes #1774 fixes #1751 fixes #1882 fixes #1795 fixes #1551 fixes #1928 fixes #1899 fixes #1646 fixes #1929

    1. ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit 50f0b4aa6a58e7fb56ab5db3f0074944122f1f17. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

      📂 See all runs for this CI Pipeline Execution


      ✅ Successfully ran 3 targets

    2. Sent with 💌 from NxCloud.

  • Review PR in StackBlitz Codeflow _Run & review this pull request in [StackBlitz Codeflow](https:///pr.new/TanStack/router/pull/1907)._

    commit: 50f0b4a

    @tanstack/history

    npm i https://pkg.pr.new/@tanstack/history@1907

    @tanstack/react-cross-context

    npm i https://pkg.pr.new/@tanstack/react-cross-context@1907

    @tanstack/react-router

    npm i https://pkg.pr.new/@tanstack/react-router@1907

    @tanstack/react-router-with-query

    npm i https://pkg.pr.new/@tanstack/react-router-with-query@1907

    @tanstack/router-cli

    npm i https://pkg.pr.new/@tanstack/router-cli@1907

    @tanstack/router-devtools

    npm i https://pkg.pr.new/@tanstack/router-devtools@1907

    @tanstack/router-generator

    npm i https://pkg.pr.new/@tanstack/router-generator@1907

    @tanstack/router-plugin

    npm i https://pkg.pr.new/@tanstack/router-plugin@1907

    @tanstack/router-vite-plugin

    npm i https://pkg.pr.new/@tanstack/router-vite-plugin@1907

    @tanstack/start

    npm i https://pkg.pr.new/@tanstack/start@1907

    @tanstack/start-vite-plugin

    npm i https://pkg.pr.new/@tanstack/start-vite-plugin@1907


    templates

  • packages/react-router/src/router.ts line 1551
    @@ -1545,3 +1543,4 @@
    15451543 location: next,
    15461544 checkLatest: () => this.checkLatest(promise),
    15471545 onReady: async () => {
    1546+ // eslint-disable-next-line ts/require-await

    since we are awaiting this, shouldn't this.startViewTransition then return a promise?

    eslint complains that there is no await 🤷

    if I remove the await and this disable comment, eslint does not complain

    Let’s do that then

  • tannerlinsleyreplied on packages/react-router/src/router.ts line 1551

    eslint complains that there is no await 🤷

  • OutdatedResolvedby schiller-manuel(GitHub profile, opens in a new tab)
  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/src/router.ts line 1551

    if I remove the await and this disable comment, eslint does not complain

  • tannerlinsleyreplied on packages/react-router/src/router.ts line 1802

    TypeScript still thinks it's necessary though, since it could have changed somehow during the second getMatch call. I hate this about TS, and it's one of the annoyances with using TS with Solid.

  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/src/router.ts line 1802

    I just noticed this when testing the PR locally... well then we'll keep it

  • OutdatedResolvedby schiller-manuel(GitHub profile, opens in a new tab)
  • SeanCassiere reviewed

    OutdatedResolvedby SeanCassiere
    Resolvedby SeanCassiere
    OutdatedResolvedby schiller-manuel(GitHub profile, opens in a new tab)
    Resolvedby SeanCassiere
    OutdatedResolvedby schiller-manuel(GitHub profile, opens in a new tab)
    OutdatedResolvedby SeanCassiere
    OutdatedResolvedby SeanCassiere
    OutdatedResolvedby tannerlinsley
    OutdatedResolvedby SeanCassiere
    OutdatedResolvedby tannerlinsley
  • SeanCassierereplied on packages/react-router/tests/routeContext.test.tsx line 417
  • SeanCassierecommented

    @tannerlinsley This in the Transitioner was previously changed from useLayoutEffect to useEffect in an attempt to fix the context issues. Here: https://github.com/TanStack/router/pull/1713/files#diff-8f39ee7f0cd19573624baf67df23085405045c8e446263697011394594e56e17R32

    For the function of subscribing to the current location, should this go back to being a useLayoutEffect? or remain as a useEffect?

  • SeanCassierereplied on examples/react/quickstart-file-based/src/routes/about.tsx line 6
  • SeanCassierereplied on packages/react-router/tests/createLazyRoute.test.tsx line 76
  • SeanCassierereplied on packages/react-router/tests/routeContext.test.tsx line 416
  • SeanCassierereplied on packages/react-router/src/router.ts line 1915
  • tannerlinsleyreplied on packages/react-router/src/router.ts line 1551

    Let’s do that then

  • Great to see this! 🌟

    Does this also aim to address #1795?

  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/tests/routeContext.test.tsx line 417

    i changed the test to expect a single call

  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/tests/routeContext.test.tsx line 396

    not necessary,I removed that comment

  • schiller-manuel(GitHub profile, opens in a new tab)replied on packages/react-router/src/index.tsx line 186

    I removed the export

  • tannerlinsleyreplied on packages/react-router/src/router.ts line 2158

    ... My gut says yes, because there can be synchronous errors that we need to catch, not just async ones

  • tannerlinsleyreplied on packages/react-router/src/router.ts line 1943

    Done

  • @tannerlinsley This in the Transitioner was previously changed from useLayoutEffect to useEffect in an attempt to fix the context issues. Here: https://github.com/TanStack/router/pull/1713/files#diff-8f39ee7f0cd19573624baf67df23085405045c8e446263697011394594e56e17R32

    For the function of subscribing to the current location, should this go back to being a useLayoutEffect? or remain as a useEffect?

    I think we should subscribe as soon as possible. So even if we need to split them up, let's do that.

  • This looks pretty good! One thing I am still struggling to understand is how does notFound() work @tannerlinsley? I have this route.tsx file which isn't an actual path, it's a scaffolding for other matched routes.

    import { createFileRoute, notFound } from '@tanstack/react-router'
    
    export const Route = createFileRoute('/_loggedin/_layout/_adminuser')({
      beforeLoad: async ({ context }) => {
        const canShow = await context.checkIfCanShow()
    
        if (!canShow) {
          throw notFound()
        }
      },
      component: () => <div>A</div>,
      errorComponent: () => <div>B</div>,
      notFoundComponent: () => <div>C</div>,
      pendingComponent: () => <div>D</div>,
    })

    I would expect/like to render "C" when notFound() is thrown, but this doesn't work. In the older versions (1.33.x), actually nothing would get rendered, the whole component tree was gone (😳). In this pull request, I see the default Not Found text and the warning

    A notFoundError was encountered on the route with ID "root", but a notFoundComponent option was not configured, nor was a router level defaultNotFoundComponent configured

    If I comment out the throw notFound() part, I see "A" rendered as expected. Any hints?

    EDIT: What's problematic here is also the fact that I never render the devtools, so I cannot inspect what's going on internally. This is true even if I implement a dummy defaultNotFoundComponent.

  • @mrlubos if you throw a notFound in a beforeLoad, only a single not found component can be shown. Currently, this PR will always invoke the root not found component and not the route's one.

    @tannerlinsley told me we could change this to show the route's not found component. Could we also make it bubble up so it chooses the nearest not found component in case the route, whose beforeLoad had thrown the notFound, does not have a not found component?

  • I'd like that @schiller-manuel. I will admit that I still have knowledge gaps around how layout routes are matched as my understanding is they're not a part of the same tree as regular routes and I thought that might be causing the problem here. Ultimately my goal is to be able to throw and handle from within the same component (I tried using the routeId option in notFound() but it made no difference). I have a layout component I'm using to check permissions for certain user type and if the user doesn't have sufficient permissions, they should be shown the 404 page without revealing the existence of specific routes.

  • chorobin(GitHub profile, opens in a new tab)replied on packages/react-router/src/router.ts line 1821

    I thought one is the current route context (routeContext) where the other is merged from the parent. At least, this is how the types work

  • chorobin(GitHub profile, opens in a new tab) approved these changes

  • This pull request can't be released fast enough. I discovered another issue introduced in 1.36.3 where a simple <Link to="/path" /> keeps reverting me back to the current path. Unfortunately, it's not obvious why or how to debug, but this problem didn't exist prior to 1.36.3, is present in the latest 1.44.4, and I can confirm is resolved in this pull request 🎉

  • KyleSanderson(GitHub profile, opens in a new tab) approved these changes

  • This pull request can't be released fast enough. I discovered another issue introduced in 1.36.3 where a simple <Link to="/path" /> keeps reverting me back to the current path. Unfortunately, it's not obvious why or how to debug, but this problem didn't exist prior to 1.36.3, is present in the latest 1.44.4, and I can confirm is resolved in this pull request 🎉

    thats sounds very similar to problems I am experiencing too! Will do a one time bonus donation if this big PR gets done 😃 ! thnks!

  • SeanCassiere reviewed

    OutdatedResolvedby tannerlinsley
    Resolvedby tannerlinsley
  • tannerlinsleyreplied on packages/history/src/index.ts line 10

    I think this one is fine as is. It should be very low traffic for a long time.

  • @tannerlinsley there is currently a bug that causes routes with loaders to sometimes (?) keep pending

    I forked the basic-file-based example and installed the package of this PR:

    https://stackblitz.com/edit/tanstack-router-2zgjft

    1. click on posts
    2. click on various posts
    3. some load and display, others keep the previous post displayed or just show a blank page

    https://github.com/user-attachments/assets/8cef0772-568d-4c56-8d11-2a58b9d63424

  • @schiller-manuel i also noticed my app became way less performant, but as always hard to say why, I didn't dig into the internals

  • i also noticed my app became way less performant, but as always hard to say why, I didn't dig into the internals

    I've noticed that with intent preloading enabled, my application hangs very noticeably during the preload (hover over a link).

  • i also noticed my app became way less performant, but as always hard to say why, I didn't dig into the internals

    I've noticed that with intent preloading enabled, my application hangs very noticeably during the preload (hover over a link).

    I have the same degraded performance outcome with preload enabled.

  • @ruisaraiva19 @sepirak-nick can you please provide a minimal complete example by forking one of the existing examples on Stackblitz that shows this performance degradation?

  • @ruisaraiva19 can you please provide a minimal complete example by forking one of the existing examples on Stackblitz that shows this performance degradation?

    Yes, I'll do it later today. Just to be clear, the performance issue happens specifically when hovering links (triggering the preloading)

  • Nice find, I believe it's the same for me, I use the same preload setting

  • I can confirm performance degradation after running a snapshot of this PR too. Will wait for @ruisaraiva19 example to provide an example for this. In the meanwhile here are some flame graphs to showcase how much the degradation is in our case:

    Old and fast: oldandfast

    New and slow: newandslow

    (Ignore the entries generated by browser addons - it doesn't look much better without them.)

  • I can confirm performance degradation after running a snapshot of this PR too. Will wait for @ruisaraiva19 example to provide an example for this. In the meanwhile here are some flame graphs to showcase how much the degradation is in our case:

    Old and fast: oldandfast

    New and slow: newandslow

    (Ignore the entries generated by browser addons - it doesn't look much better without them.)

    I’m seeing some function calls that I recently removed in the latest version of this PR. Is this up to date?

  • @schiller-manuel @tannerlinsley Here is a bare minimum demo based on the kitchen-sink-react-query-file-based example. The Dashboard index route is the one with the code changes.

    A couple of notes regarding the demo:

    • At first, I couldn't recreate the performance issue with a bare kitchen-sink-react-query-file-based example (maybe too simple of an app?).
    • After debugging with a couple of console logs + the React Profile flame graphs, I noticed that route components are rerendered multiple times when hovering links.
    • With defaultPreload: false, route components would render 5 times on the first hover event and 3 times on the following hover events.
    • With defaultPreload: 'intent', route components would render 7 times on the first hover event and 5 times on the following hover events.
    • So with defaultPreload: 'intent', we get a few extra rerenders.
    • This can be confirmed by looking at the console logs when inside the Dashboard index route.
    • My experience with the performance issue happens on a web app with complex dashboards.
    • To confirm this with a minimal demo, I created a SlowComponent component that takes at least 5ms to render to simulate a complex component.
    • After rendering a few component instances, I reproduced the same performance issue when hovering over links like on the web app mentioned above. You can check that when hovering over a link, the color change (from blue to red) is not responsive.
    • Then, I wrapped the DashboardIndexComponent component in a React.memo and the rerender performance issue is gone since the route component does not rerender on the hover events anymore.
    • I tried the React.memo approach on the web app mentioned above and it fixed the issue as well which uses version 1.43.10 of the router.

    To summarize, it seems that when hovering over links the router is rerendering pretty much all Outlet components, hence the React.memo approach to fix the issue.

    Please let me know if any of my conclusions aren't correct and feel free to ask any questions!

  • Good find. I can take a look at these perf regressions later tonight.

  • I’m seeing some function calls that I recently removed in the latest version of this PR. Is this up to date?

    I just remembered i pinned a certain commit. Will redo this without pinning a certain commit but pinning the PR today or tomorrow.

  • Made another comparison with our app now without the addons. Result is pretty much the same in terms of the factor of the performance degradation. The addons just add overall. It's still a performance degradation compared to the old version from last months npm update cycle we did, even with the newer commit.

    1.34.9: oldandfast2

    commit 64a8bdd: newandslow2

  • Sweet. I’ll take a look. I already have a good idea of where to start.

    Tanner Linsley TanStack LLC On Jul 14, 2024 at 12:17 PM -0600, martylukyy @.***>, wrote:

    Made another comparison with our app now without the addons. Result is pretty much the same in terms of the factor of the performance degradation. The addons just add overall. It's still a performance degradation compared to the old version from last months npm update cycle we did, even with the newer commit. 1.34.9: oldandfast2.png (view on web) commit 64a8bdd: newandslow2.png (view on web) — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

  • I just pushed a few fixes to the MatchInner reactivity. Can you wait for that PR version to release and give it a shot?

  • I just pushed a few fixes to the MatchInner reactivity. Can you wait for that PR version to release and give it a shot?

    Looks a lot better to me now! Performance measurement is done without addons again to get a better read on it. Here is another flame graph:

    newandfast

  • I just pushed a few fixes to the MatchInner reactivity. Can you wait for that PR version to release and give it a shot?

    Looks a lot better to me now! Performance measurement is done without addons again to get a better read on it. Here is another flame graph:

    newandfast

    Better or worse than original?

  • Let me build 2 versions real quick so that devtools won't interfere that much in the measurement and get back to you in a few minutes!

  • It's a worse by a one digit millisecond margin compared to 1.34.9. (Might also be variance..) Didn't attach a flame graph of the production versions since there isn't much to read out of there. If you want numbers we are talking about 155ms +- 5ms scripting time measured between 1.34.9 and 0f34fbe.

    Chances are, this difference comes from other changes made since 1.34.9 and it's on par with what it used to be.

    Given that it solves so many bugs it's better than the original. 😄

  • Perfect! Just need to get tests passing and we’re good. I don’t have anything else to push.

    • After debugging with a couple of console logs + the React Profile flame graphs, I noticed that route components are rerendered multiple times when hovering links.

    To summarize, it seems that when hovering over links the router is rerendering pretty much all Outlet components, hence the React.memo approach to fix the issue.

    @tannerlinsley regarding the above mentioned. Is my assumption correct? If so, is this behavior expected (by design) or not really?

    • After debugging with a couple of console logs + the React Profile flame graphs, I noticed that route components are rerendered multiple times when hovering links.

    To summarize, it seems that when hovering over links the router is rerendering pretty much all Outlet components, hence the React.memo approach to fix the issue.

    @tannerlinsley regarding the above mentioned. Is my assumption correct? If so, is this behavior expected (by design) or not really?

    It's partially correct. It should be rerendering any components that are subscribed to parts of the state that actually change. This is why we built the @tanstack/store library and the @tanstack/react-store adapter. It uses useSyncExternalStore and fine-grained selectors to determine if the "slice" that we return from the selector has changed (even shallowly compared). If it is, then it's rerendered.

    In the case that I just fixed, we were returning a slice in <InnerMatch> that was constantly changing identities.

  • @tannerlinsley is there any checklist on what's remaining until this gets merged? Do you think it will be released by the next week?

  • schiller-manuel(GitHub profile, opens in a new tab) approved these changes

  • Sign in to join this conversation.