-
Notifications
You must be signed in to change notification settings - Fork 1.4k
docs: how to delay Qwik core execution #8041
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
packages/docs/src/routes/docs/(qwik)/advanced/core/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # Core | ||
|
|
||
| In order to be super fast at load time Qwik only load the framework itself (let's call it "core") when it needs it. | ||
|
|
||
| If you want to squeeze the last bit of loadtime performance, you can delay its execution until you really need it. | ||
|
|
||
| ⚠️ Delaying core execution should **not** affect your UX or devX, this is an advance performance trick ⚠️ | ||
maiieul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## useVisibleTask$ | ||
|
|
||
| `useVisibleTask$` will **always** execute core before it's callback is called. | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```jsx | ||
| // Requires core when component is visible in the viewport | ||
| useVisibleTask$(() => { | ||
| console.log('Hello core'); | ||
| }); | ||
|
|
||
| // Requires core on requestIdleCallback | ||
| useVisibleTask$( | ||
| () => console.log('Hello core'), | ||
| { strategy: 'document-idle' } | ||
| ); | ||
| ``` | ||
|
|
||
| In **some** cases you can replace `useVisibleTask$` with either `useOn` or `useTask$` | ||
|
|
||
| ## useOn | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Replace `useVisibleTask$` with `useOn('qvisible')` / `useOn('qidle')` if | ||
|
|
||
| - You only need to trigger a callback once | ||
| - The code must run on the client | ||
|
|
||
| `useOn`, `useOnDocument` & `useOnWindow` execute core if they use a variable from the component scope : | ||
|
|
||
| ```jsx | ||
| import { libId } from 'library'; | ||
| const globalId = 'global-id'; | ||
| const Component = component$(() => { | ||
| const ref = useSignal(); | ||
| const id = useId(); | ||
|
|
||
| // Executes core at load time | ||
| useOn('qidle', $(() => console.log(ref))); | ||
| // Executes core at load time | ||
| useOn('qidle', $(() => console.log(id))); | ||
| // Do not execute core at load time | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| useOn('qidle', $(() => console.log(globalId))); | ||
| // Do not execute core at load time | ||
| useOn('qidle', $(() => console.log(libId))); | ||
| // Do not execute core at load time | ||
| useOn('qidle', $(() => console.log('id'))); | ||
maiieul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <p ref={ref}></p> | ||
| <p id={id}></p> | ||
| <p id={globalId}></p> | ||
| <p id={libId}></p> | ||
| <p id="id"></p> | ||
| ) | ||
| }) | ||
| ``` | ||
|
|
||
| ## useTask$ | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Replace `useVisibleTask$` with `useTask$` if | ||
|
|
||
| - You need to listen on state changes | ||
| - The code can execute on the server | ||
|
|
||
| ```jsx | ||
| const Component = component$(() => { | ||
| const search = useSignal(); | ||
| // Do not execute core at load time | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| useTask$(({ track }) => { | ||
| track(search); | ||
| console.log(search); | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| return <input bind:value={search} type="search" />; | ||
| }); | ||
| ``` | ||
|
|
||
| ## useOn + useTask$ | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| A classic usecase of `useVisibleTask$` is to start listening on browser specific event: | ||
GrandSchtroumpf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```jsx | ||
| const isMobile = useSignal(false); | ||
| useVisibleTask$(({ cleanup }) => { | ||
| const query = window.matchMedia('(max-width: 400px)'); | ||
| const handler = (event) => { | ||
| isMobile.value = event.matches; | ||
| }; | ||
| query.addEventListener('change', handler); | ||
| cleanup(() => query.removeEventListener('change', handler)); | ||
| }); | ||
| ``` | ||
|
|
||
| In this case we actually need core when the handler is triggered. Here is how to delay core execution : | ||
|
|
||
| ```jsx | ||
| const isMobile = useSignal(false); | ||
| // On idle, start listening on the event | ||
| useOn( | ||
| 'qidle', | ||
| $(() => { | ||
| const query = window.matchMedia('(max-width: 400px)'); | ||
| // Store mediaQuery & handler to cleanup later | ||
| globalThis['media-query:(max-width: 400px)'] = { | ||
| query, | ||
| handler: (event) => { | ||
| // Forward the event to the document | ||
| const copy = new e.constructor('media-query:(max-width: 400px)', event); | ||
| document.dispatchEvent(copy); | ||
| }, | ||
| }; | ||
| query.addEventListener('change', globalThis['media-query:(max-width: 400px)'].handler); | ||
| }) | ||
| ); | ||
|
|
||
| // useOnDocument execute core when it actually needs it | ||
| useOnDocument( | ||
| 'media-query:(max-width: 400px)', | ||
| $((event) => { | ||
| isMobile.value = event.matches; | ||
| }) | ||
| ); | ||
|
|
||
| // useTask$ is used to cleanup event listeners | ||
| useTask$(({ cleanup }) => { | ||
| cleanup(() => { | ||
| if (!globalThis['media-query:(max-width: 400px)']) return; | ||
| const { query, handler } = globalThis['media-query:(max-width: 400px)']; | ||
| query.removeEventListener('(max-width: 400px)', handler); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| As we can see, this is a LOT of work, and it's not a great dev experience ! | ||
maiieul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.