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

Fixing Transitions #91

Closed
wants to merge 4 commits into from
Closed

Conversation

webdevcody
Copy link
Contributor

There was still a little bug related to transitions. Basically the useServerAction would invoke onSuccess before the server action was done running revalidatePath or redirect. This refactoring includes

  • removing the executing boolean since a transition already tracks the action state
  • using an effect to listen for the isTransitioning flag to know exactly when we should be calling onSuccess, onError, and resolving the original promise with the results
  • refactoring the approach for resolving the promise by storing it in a ref and resolving it in the effect

if I get time, I want to add some type of testing to verify this bug won't happen again. The logic for this useServerAction is a bit complex, and I'm worried it's going to start becoming bug food as things are refactored or changed. Unforatnely I think this type of testing would require playwright or cypress because the testing would require verifying the hooks work with the underlying revalidatePath and redirect methods in combination to delays in RSCs refetching data.

…transition to finish and then invoke the onSuccess callbacks
Copy link

changeset-bot bot commented Jun 5, 2024

⚠️ No Changeset found

Latest commit: 4859987

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jun 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zsa ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 5, 2024 6:23pm

@IdoPesok
Copy link
Owner

IdoPesok commented Jun 5, 2024

Good catch, appreciate the help with all this transition stuff. Wish startTransition accepted a callback or something so we didn't need this useEffect stuff but seems like this is what we got to do.

Concur with the testing -- @WesternConcrete got client side tests going on #75 but it seems like, as you said, testing redirect might be out of Jest's scope.

The one thing I am thinking about is how this affects the retry option. I originally kept both isExecuting and isTransitioning originally so that, when retrying, isExecuting would still be true. It seems like now the status is no longer pending when it is retrying. Here is a snippet you can try:

  const { isPending, execute, data, error, isError } = useServerAction(
    incrementNumberAction,
    {
      retry: {
        maxAttempts: 3,
        delay: 1000,
      },
    }
  )

Happy to merge this once the retry functionality is stable. The changes look good and solve the onSuccess bug.

@webdevcody
Copy link
Contributor Author

@IdoPesok double check when you get a chance, I tested on my own app with retries and without, it seems to continue to show the loader until after all the retries finish

@IdoPesok IdoPesok mentioned this pull request Jun 5, 2024
@IdoPesok
Copy link
Owner

IdoPesok commented Jun 5, 2024

Added test cases for retrying in #92 , closing this PR as these commits are in there.

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.

2 participants