Skip to content
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

fix: RecordVersionsList fetch versions only once #2664

Merged
merged 1 commit into from May 17, 2024

Conversation

ptamarit
Copy link
Member

@ptamarit ptamarit commented May 6, 2024

❤️ Thank you for your contribution!

Description

Opening the landing page of any record currently fetches 4 times the list of versions from the API:


Versions API being fetched multiple times


The problem is due to the fetchVersions and cancellableFetchVersions (wrongly named cancellableFetchCitation) functions being recreated at every render (when the state changes).

This pull request moves the fetching functions inside useEffect, so that they are created only within the scope of useEffect and don't need to be declared as a dependency. The version is now only fetched once (since the props on which recordDeserialized.links.versions and recid don't change).

Checklist

Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:

Frontend

Reminder

By using GitHub, you have already agreed to the GitHub’s Terms of Service including that:

  1. You license your contribution under the same terms as the current repository’s license.
  2. You agree that you have the right to license your contribution under the current repository’s license.

@ptamarit ptamarit requested a review from jrcastro2 May 6, 2024 08:53
Copy link
Member

@slint slint left a comment

Choose a reason for hiding this comment

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

LGTM, I assume passing the correct dependencies to useEffect is the main fix :)

Copy link
Contributor

@jrcastro2 jrcastro2 left a comment

Choose a reason for hiding this comment

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

🚀 Nice fix!

@ptamarit
Copy link
Member Author

ptamarit commented May 7, 2024

LGTM, I assume passing the correct dependencies to useEffect is the main fix :)

  • In theory, an empty array means no dependency and no need to re-run the effect again, so it's not 100% clear to me why it was running on every render.
  • Had we not disabled the the ESLint warning, we would have had to define the fetching function as a dependency, and then it would have been clear that the useEffect would always be triggered due to the function being new at every render.
  • Moving the fetch function inside the useEffect was enough to fix the problem, which means that the fact that this function was always a new version (and therefore different) was what was triggering the useEffect (even though it was not declared as a dependency).
  • I decided to define the dependencies as it did not hurt and is the right thing to do.

@slint slint merged commit 17a1775 into inveniosoftware:master May 17, 2024
2 checks passed
@ptamarit ptamarit deleted the fix-fetch-versions-only-once branch May 17, 2024 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants