qwik icon indicating copy to clipboard operation
qwik copied to clipboard

[🐞] useTask$/useVisibleTask$ produce unexpected behavior

Open cmbartschat opened this issue 3 years ago • 12 comments

Which component is affected?

Qwik Runtime

Describe the bug

The behavior I'm seeing is that a server-side useTask$ is running, but the changes are not being applied to the rendered HTML that is sent to the client. The cleanup callback is also being called immediately which seems unintended.

More detail in the repro steps.

Reproduction

https://stackblitz.com/edit/qwik-starter-d4eggl?file=src/routes/index.tsx

Steps to reproduce

  1. Open the StackBlitz
  2. Start the server (npm run preview)
  3. Open the site
  4. See that the status is init rather than mounted.
  5. See that using <Link> to navigate away does not switch to the unmounted status
  6. See that using <Link> to navigate back does end up in the mounted state, then unmounted after that
  7. Uncomment the useVisibleTask$ in src/routes/index.tsx
  8. Restart the server
  9. See that the server rendered page is still status: init (browser JS turned off), but if JS is enabled, ends up as unmounted (server) + visible, which is also surprising

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @builder.io/qwik: ^1.1.2 => 1.1.2 
    @builder.io/qwik-city: ^1.1.2 => 1.1.2 
    undici: 5.22.0 => 5.22.0 
    vite: 4.3.3 => 4.3.3 

cmbartschat avatar May 13 '23 22:05 cmbartschat

this is a limitation of streaming rendering. in order to make this work it would require to rerender a parent. It could lead to infinite loops, causing DDoS (no framework support this). On top of that, it would require to disable streaming, since eventually any children can change the root node. we can't send anything to the DOM.

I wonder how we can educate developers on this:

  • Docs
  • Some warnings?

Any thoughts?

manucorporat avatar May 14 '23 14:05 manucorporat

All cleanup are called when the application is paused. Imagine a server does setTimeout(), we need to cleanup timer etc. it's expected behaviour

manucorporat avatar May 14 '23 14:05 manucorporat

Ok great, thanks for the extra context.

My understanding of useTask was missing the detail that a useTask's cleanup is only called on component unmount if the task ran a second time on the client. My mental model is still adjusting.

However - there is something potentially actionable.

The serialized state we send to the client is the state after the child component's task has been processed, rather than immediately after the parent component is "finalized", let's say. This explains why we're seeing init in the html, but then using a useVisibleTask$, instead of getting init + visible, we get unmounted (server) + visible. Maybe this could be changed.

Then yeah, in terms of education, definitely warnings, maybe even erroring out on dev?

"State updates cannot be triggered from a component's useTask$ if it received the state through context or props."

cmbartschat avatar May 14 '23 16:05 cmbartschat

"State updates cannot be triggered from a component's useTask$ if it received the state through context or props."

this would be a huge limitation during normal browser execution.

how about something like this:

This state mutation would trigger a rerender a component that is already rendered, which is not possible during SSR.

SSR HTML might not be in sync with the serialized state.

manucorporat avatar May 14 '23 20:05 manucorporat

My understanding of useTask was missing the detail that a useTask's cleanup is only called on component unmount if the task ran a second time on the client. My mental model is still adjusting.

The cleanup is called when:

  • the same task runs again
  • the component is unmounted
  • the container is paused

manucorporat avatar May 14 '23 20:05 manucorporat

The reason is that it's usually necesary to cleanup things when moving between server and client, imagine, a setTimeout will not cross from the server to the client

manucorporat avatar May 14 '23 20:05 manucorporat

I like your message. Yeah didn't mean for mine to imply anything about client-side behavior. I wonder if we could be sophisticated enough to know if an already-"finalized" component is subscribed to that state or not. Thinking of a case where a top-level parent defines a store, a subcomponent updates the store in useTask$, then another component renders that store. There's definitely some weirdness here, see playground

cmbartschat avatar May 14 '23 20:05 cmbartschat

And then adding a client-side state update we see that the actual store value is unmounted - playground

cmbartschat avatar May 14 '23 20:05 cmbartschat

By the way, there isn't always a cleanup to run when a component is unmounted, which is the thing I learned. This playground shows that - the first time the component is unmounted, since the task never ran on the client, there's no cleanup to do.

cmbartschat avatar May 14 '23 20:05 cmbartschat

this is a limitation of streaming rendering. in order to make this work it would require to rerender a parent. It could lead to infinite loops, causing DDoS (no framework support this).

Could the server serialize updated state at the end of document (based on just initial render, without re-rendering components) and the Qwikloader resumes some components based on the updated state?

Worst case scenario the whole tree needs to be resumed client side when document is loaded, but at least things would work as expected. We could show some warning when this behavior occurs and developer could optimize.

aivarsliepa avatar May 15 '23 12:05 aivarsliepa

This issue is really old and it's stale, there are few suggestions though. So I'm closing this issue for now, feel free to re-open it if it's still an issue for you.

gioboa avatar Nov 28 '24 11:11 gioboa