fix: Optimize the revalidation logic for same key requests.#4138
Merged
fix: Optimize the revalidation logic for same key requests.#4138
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
shuding
approved these changes
Jul 2, 2025
Member
shuding
left a comment
There was a problem hiding this comment.
This is a great optimization, thanks!
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The SWR hook was experiencing performance issues due to inefficient request deduplication logic, causing unnecessary re-renders and redundant API calls when multiple components used the same key.
Solution
Added a performance optimization at line 644 that checks if a request is already in progress before initiating a new one:
Key Changes
FETCH[key]exists, preventing duplicate revalidations for the same keyThis optimization ensures that when multiple components request the same data simultaneously, only one actual fetch request is made, improving overall application performance.
Testing
@gdborton created the
e2e/site/app/perf/page.tsxas an example of the problem. I then created a playwright test that asserts that the "Expensive Component" should be rendered under 1 second.Before the fix
$ pnpm test:e2e perf --repeat-each 10 -j 1
After the fix
$ pnpm test:e2e perf --repeat-each 10 -j 1
(Mobile) Deployments List page testing (before and after)
Screen.Recording.2025-06-27.at.9.49.23.AM.mov
(Desktop) Deployments List page testing (before and after)
Screen.Recording.2025-06-26.at.2.18.02.PM.mov