Unlocking the Magic of Fine-Grained Reactivity in Qwik
Introduction
Reactivity is a fundamental concept in modern frontend frameworks, referring to the automatic update of the user interface in response to changes in the application's state. Among the popular reactive frameworks like React, Angular, Svelte, and Vue, Qwik stands out as a fine-grained reactive framework. Although Solid is currently considered the best in terms of executing components only once, Qwik's unique advantage lies in its resumability feature. Unlike hydration in other frameworks, Qwik can serialize the reactivity graph into HTML, reducing the need to download most of the application code on startup. If you want to delve deeper into how Qwik serializes closures into HTML, you can find my previous post on the subject here.
Serialize, Then Resume
Qwik's approach to achieving reactivity involves a three-step process: creating the reactivity graph during SSR/SSG, serializing the graph into HTML, and finally, resuming the reactivity graph on the client side. Let's take a closer look at each step.
1. Creating the Reactivity Graph
In Qwik, the reactivity graph is created during SSR/SSG by executing the components once. Here's an example of explicit reactivity by using useComputed$
:
2. Serialization of the Reactivity Graph
Once the reactivity graph is constructed, Qwik serializes it into a JSON format. This allows Qwik to efficiently transmit the reactivity graph as part of the initial HTML response, reducing the need for extensive code downloads on the client-side. Here's an example of the serialized reactivity graph:
3. Resuming the Reactivity Graph on the Client Side
After the initial HTML response is received on the client-side, Qwik resumes the reactivity graph using the serialized data. This process allows the user interface to be interactive and respond to user interactions just as it would in a traditional reactive application.
Subscription Management
Qwik employs a runtime tracking technique to maintain connections between various reactive nodes. Whenever a reactive expression is re-run, Qwik rebuilds its dependencies to ensure accurate reactivity. The subscription management in Qwik consists of two primary components:
-
Invoke Context: This component is responsible for keeping track of any running reactions or derivations.
-
Subscription Manager: This component handles the creation and removal of subscriptions as needed during the reactivity process.
Running Tasks
In addition to the core primitive useSignal()
, Qwik provides other utilities like useComputed$()
, useResource()
, and useTask()
. When these tasks are invoked, Qwik clears the subscriptions using the subscription manager and creates a new invoke context with a subscriber before executing the tasks. Here's a snippet illustrating the process:
Rendering Components
When rendering a component, Qwik clears the subscriptions using the subscription manager and creates a new invoke context with a subscriber before executing the component. This ensures that components respond correctly to changes in state. Here's an example:
Updating the Global Context through invoke()
To keep track of the running reaction, Qwik creates the context stack using the invoke()
function. This ensures that the correct context is maintained during the execution of reactive expressions. Here's how invoke()
is used:
Creating New Subscriptions
When reading from a signal or proxy in Qwik, the framework attempts to get the subscriber from the current invoke context and then adds it to its local subscription manager. This ensures that the reactivity chain remains correct. Here's how Qwik handles it:
Structural Changes
While Qwik excels at fine-grained reactivity, there are certain limitations when it comes to describing structural changes within Signals. Structural changes involve adding or removing DOM nodes, and currently, Qwik cannot directly describe these changes within the reactive context. As a result, in scenarios where structural changes occur, Qwik is forced to download and re-execute the relevant component to maintain the reactivity graph.
Let's explore a couple of examples to better understand the challenges and possible workarounds.
Example: <Resource />
Consider the following example where the ExplicitUseResource
component utilizes the useResource$()
hook to fetch data and render a list of Pokemon based on the count
Signal:
In this case, because the number of Pokemon displayed in the list changes based on the count
Signal, Qwik faces difficulty in describing the structural changes effectively. As a result, Qwik is required to download the component QRL of ExplicitUseResource
whenever the count Signal changes, leading to re-execution of the component.
Example: Remove Component conditionally
Now, consider the following example where the RemoveComponentConditionally
component conditionally renders the Display
component based on the count
Signal:
In this case, when the value of count
is less than 3, the Display component is rendered. However, because Qwik cannot fully describe the conditional rendering within the reactivity graph, it is forced to download the component QRL of RemoveComponentConditionally
whenever the value of count
changes.
Conclusion
In conclusion, Qwik stands as a fine-grained reactive framework similar to SolidJS, it directly updates the DOM upon changes in the application's state. This level of reactivity ensures a seamless and highly responsive user interface. While there're still some cases where Qwik needs to execute the whole component, it excels at delivering precise and surgical reactivity, limiting updates to only the necessary portions of the DOM.
However, the true magic of Qwik lies in its ingenious approach to reactivity. Fine-grained reactivity necessitates that all components execute at least once to establish the reactivity graph. Qwik brilliantly capitalizes on the fact that components have already been executed on the server during SSR/SSG. By serializing this reactivity graph into HTML, Qwik bestows the client with the extraordinary ability to bypass the initial "execute the world to learn about the reactivity graph" phase entirely. This unique feature is known as "resumability."
Thanks to resumability, the client can launch the application instantaneously without the need for rehydration and component code downloads. The reactive components are already primed and ready, precisely as they were on the server. This translates into an instant startup experience, where users can swiftly interact with the application without delay.
Reference
- Ryan Carniato - A Hands-on Introduction to Fine-Grained Reactivity
- Ryan Carniato- Building a Reactive Library from Scratch
- MIŠKO HEVERY - My Take on a Unified Theory of Reactivity
- MIŠKO HEVERY - A Brief History of Reactivity
- MIŠKO HEVERY - Unveiling the Magic: Exploring Reactivity Across Various Frameworks