Skip to content

fix(router): context issues#1907

Merged
schiller-manuel merged 41 commits intomainfrom
fix-context-issues
Jul 16, 2024
Merged

fix(router): context issues#1907
schiller-manuel merged 41 commits intomainfrom
fix-context-issues

Conversation

@tannerlinsley
Copy link
Copy Markdown
Member

@tannerlinsley tannerlinsley commented Jul 6, 2024

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

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented Jul 6, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 50f0b4a. 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

Sent with 💌 from NxCloud.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jul 6, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

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

location: next,
checkLatest: () => this.checkLatest(promise),
onReady: async () => {
// eslint-disable-next-line ts/require-await
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint complains that there is no await 🤷

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s do that then

Comment thread packages/react-router/src/router.ts Outdated
this currently fails!

AssertionError: expected "spy" to be called 2 times, but got 1 times
 ❯ tests/routeContext.test.tsx:417:18
    415|
    416|     // Expect double call: once from preload, once from navigate
    417|     expect(mock).toHaveBeenCalledTimes(2)
       |                  ^
    418|   })
    419|
Comment thread packages/react-router/tests/routeContext.test.tsx Outdated
Comment thread examples/react/quickstart-file-based/src/routes/about.tsx Outdated
Comment thread packages/react-router/tests/createLazyRoute.test.tsx
Comment thread packages/react-router/tests/routeContext.test.tsx Outdated
Comment thread packages/react-router/tests/routeContext.test.tsx
Comment thread packages/react-router/src/index.tsx Outdated
Comment thread packages/react-router/src/router.ts Outdated
Comment thread packages/react-router/src/router.ts Outdated
Comment thread packages/react-router/src/router.ts Outdated
Comment thread packages/react-router/src/router.ts Outdated
Comment thread packages/react-router/src/router.ts Outdated
@SeanCassiere

This comment was marked as resolved.

* chore(examples): cleanup the about route in "quickstart-file-based"

* style(react-router): eslint on "createLazyRoute.test.tsx"

* test(react-router): add a test to "createLazyRoute" to check that the heavy component is rendered in the DOM using testing-library

* test(react-router): duplicate the preload test using screen rendering from testing library

* test(react-router): forgot to add preload intent

* chore(react-router): add the comment above the `runBeforeLoad` call stating that we're actually running it

* chore(react-router): add comment above the `runLoader` call

* chore(react-router): remove the destructuring of `p`

* style(react-router): disable these eslint warnings in the `router.ts` file

* chore(react-router): undo the `params: p` destucturing change
@ruisaraiva19
Copy link
Copy Markdown
Contributor

  • 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?

@tannerlinsley
Copy link
Copy Markdown
Member Author

  • 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.

@mrlubos
Copy link
Copy Markdown

mrlubos commented Jul 15, 2024

@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
Copy link
Copy Markdown
Contributor

this needs to be fixed
#1907 (comment)

and of course test need to pass

tannerlinsley and others added 7 commits July 15, 2024 18:01
…ime of preload**

This fixes a special case where you could hover over a link and start a preload, then click the link before it resolved. Before, any preload updates (including a successful state update) were ignored if the match was active, so in this case, the newly active match that hadn't resolved yet would never get updated since it is now active.

This change only disallows updates to the original match IDs that are currently active.
@schiller-manuel
Copy link
Copy Markdown
Contributor

@tannerlinsley this was probably a mistake so I added that any back:

2a85a58

@schiller-manuel schiller-manuel self-requested a review July 16, 2024 17:30
@schiller-manuel schiller-manuel merged commit 3003d98 into main Jul 16, 2024
@schiller-manuel schiller-manuel deleted the fix-context-issues branch July 16, 2024 17:34
@mrlubos
Copy link
Copy Markdown

mrlubos commented Jul 16, 2024

Thank you 🕺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment