-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: If newData
is deeply to the latest state, broadcast the latest state
#1697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shuding
merged 1 commit into
vercel:main
from
icyJoseph:bugfix/re-render-when-revalidate-sees-equal-data
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good fix! And I just took a deeper look and it seems the problem is caused by here:
swr/src/use-swr.ts
Line 372 in 4663154
When they're not equal, we broadcast the new data even if the new data is not defined. So another way to fix it is adding:
to that line of code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any preference? I guess one
if
branch vsif-else
is more terse?Also, I commented heavily here, but perhaps I should revert to the original comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have misunderstood but
Still requires the
else
block right (the test still fails otherwise)? We need to bindnewState.data
tostateRef.current.data
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry I meant adding it here:
setState( mergeObjects( { error: updatedError, isValidating: updatedIsValidating }, // Since `setState` only shallowly compares states, we do a deep // comparison here. + isUndefined(newData) || compare(stateRef.current.data, updatedData) ? UNDEFINED : { data: updatedData } ) )
I prefer this for now because it is more universal, so we have a way to call
broadCastState()
but skip updating the data, if it's undefined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that too, and it caused a couple other tests to failed, iirc.
Not on my laptop now, but I'll retry first thing in the morning if that's ok.
I also agree that placing the check there seems more correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if that doesn’t work we can publish your original fix for sure 👍
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test cases that stop working are:
The test description kinda makes it clear why, because when mutating to
undefined
, with the proposed changed we'd be broadcasting the stale state instead.Since the proposal didn't work out, I also tried:
But this breaks two tests 😅 the first one is introduced on this MR, it implicitly reintroduces the initial issue.
I think it'd be an ok trade-off to patch this now, and investigate further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, yeah that makes total sense!