You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The above implementation causes more JavaScript to load eagerly, rather than responding precisely to user events. Increased upfront JavaScript loading results in slower app performance.
143
+
The above implementation causes more JavaScript to load eagerly, rather than responding precisely to user events. Increased upfront JavaScript loading results in slower app performance. See [below](#delaying-core-execution) for more details.
144
144
145
145
Instead, use the `useOnDocument()` hook to register events on the `document` object, this way Qwik will not execute any JS until the event is triggered.
146
146
@@ -198,3 +198,144 @@ However, exercise caution! If the required information (such as query parameters
198
198
This approach helps to prevent eager loading of JavaScript and improves performance.
At load time we want to execute as little as possible to free main thread for other priority. With Qwik you can even delay the framework execution (called "core"), but there are some rules to follow.
205
+
206
+
Some things to keep in mind before checking the rules :
207
+
- Delaying core execution usually comes at the price of devX, this is an advance performance trick.
208
+
- Like other chunks, core will be preloaded so the app doesn't have to load it at execution time.
209
+
210
+
### useVisibleTask$
211
+
212
+
`useVisibleTask$` will **always** execute core before its callback is called.
213
+
214
+
```jsx
215
+
// Requires core when component is visible in the viewport
216
+
useVisibleTask$(() => {
217
+
console.log('Hello core');
218
+
});
219
+
220
+
// Requires core on requestIdleCallback
221
+
useVisibleTask$(
222
+
() =>console.log('Hello core'),
223
+
{ strategy:'document-idle' }
224
+
);
225
+
```
226
+
227
+
In **some** cases you can replace `useVisibleTask$` with either `useOn` or `useTask$`
228
+
229
+
#### useOn
230
+
231
+
Replace `useVisibleTask$` with `useOn('qvisible')` / `useOn('qidle')` if
232
+
233
+
- You only need to trigger a callback once
234
+
- The code must run on the client
235
+
236
+
`useOn`, `useOnDocument` & `useOnWindow` execute core if they use a variable from the component scope :
As we can see, this is a LOT of work, and it's not a great dev experience ! But if you're building a library or just trying to go as lean as possible, this is possible.
0 commit comments