Optimize the revalidation logic for same key requests. #4138
+191
−9
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.tsx
as 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