Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[🐞] V2: Bug when signal of useComputed$ is used in useTask$ or useVisibleTask$ #7213

Open
fabian-hiller opened this issue Dec 30, 2024 · 5 comments · May be fixed by #7435
Open

[🐞] V2: Bug when signal of useComputed$ is used in useTask$ or useVisibleTask$ #7213

fabian-hiller opened this issue Dec 30, 2024 · 5 comments · May be fixed by #7435
Assignees

Comments

@fabian-hiller
Copy link
Contributor

fabian-hiller commented Dec 30, 2024

Which component is affected?

Qwik Runtime

Describe the bug

When a useComputed$ signal is read in useVisibleTask$, the task is executed twice (or multiple times if multiple useComputed$ are used). Also, useComputed$ is unnecessarily re-executed afterwards.

Reproduction

https://stackblitz.com/edit/github-o2f6fj9n-meq2plo7?file=src%2Froutes%2Findex.tsx

Steps to reproduce

  1. Open StackBlitz link
  2. Open and clear browser console
  3. Reload Qwik App

Now you should see this in the console:

useVisibleTask$: Do stuff with text...
useVisibleTask$: Do stuff with text...
useComputed$: Computing initial text...

The expected output would be:

useVisibleTask$: Do stuff with text...

This is because useComputed$ should only run once on the sever and useVisibleTask$ should only run once in the browser. A similar bug exists when using useTask$.

System Info

-

Additional Information

No response

@fabian-hiller fabian-hiller added STATUS-1: needs triage New issue which needs to be triaged TYPE: bug Something isn't working labels Dec 30, 2024
@Varixo Varixo added COMP: runtime VERSION: upcoming major and removed STATUS-1: needs triage New issue which needs to be triaged labels Dec 31, 2024
@Varixo Varixo self-assigned this Dec 31, 2024
@Varixo Varixo moved this to In progress in Qwik Development Dec 31, 2024
@Varixo
Copy link
Member

Varixo commented Jan 11, 2025

Analysis:
in v2 when the QRL is not resolved at the time when it is needed we throw a promise and retry when the promise is resolved. The text.value was not needed before, so it will cause to throw and rerun the task.

Expected output:

 useVisibleTask$: Do stuff with text...
 useComputed$: Computing initial text...

The computed should execute on client, because in v2 it is lazy and on the server it was not needed.

Workaround:

  useVisibleTask$(async () => {
    console.log(text.value); // <-- read the computed value first
    console.log('useVisibleTask$: Do stuff with text...'); <-- then execute the rest
  });

We should probably think how to solve this anyway

@fabian-hiller
Copy link
Contributor Author

Thanks Michał! There is a similar "bug" with useTask$. Does the same apply here?

@fabian-hiller
Copy link
Contributor Author

Yes, I think you are right. My bad.

@Varixo Varixo linked a pull request Mar 17, 2025 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.

2 participants